运行时检查失败#0 - ESP的值在函数调用中未正确保存 [英] Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call

查看:1496
本文介绍了运行时检查失败#0 - ESP的值在函数调用中未正确保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的程序,演示了我使用Qt应用程序使用多继承的运行时错误。继承树看起来像:

I created a simple program that demonstrates the runtime error I'm getting with my Qt application that uses multiple inheritance. The inheritance tree looks like:

QGraphicsItem (abstract)
      \
     QGraphicsLineItem      MyInterface (abstract)
                 \          /
                  \        /
                  MySubclass

代码:

/* main.cpp */
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsLineItem>

//simple interface with one pure virtual method
class MyInterface
{
public:
  virtual void myVirtualMethod() = 0;
};

//Multiple inheritance subclass, simply overrides the interface method
class MySubclass: public QGraphicsLineItem, public MyInterface
{
public:
  virtual void myVirtualMethod() { }
};

int main(int argc, char** argv)
{
  QApplication app(argc, argv); //init QApplication
  QGraphicsScene *scene = new QGraphicsScene(); //create scene

  scene->addItem(new MySubclass()); // add my subclass to the scene

  Q_FOREACH(QGraphicsItem *item, scene->items()) // should only have one item
  {
    MyInterface *mInterface = (MyInterface*)item; // cast as MyInterface
    mInterface->myVirtualMethod(); // <-- this causes the error
  }
  return 0;
}

在调用Visual Studio时调试我的接口方法会导致运行时错误:

Debugging in visual studio results in a runtime error when my interface method is called:

    Run-Time Check Failure #0 - The value of ESP was not properly 
    saved across a function call.  This is usually a result of 
    calling a function declared with one calling convention with 
    a function pointer declared with a different calling convention.

任何想法是什么问题?

推荐答案

因为你使用多重继承, vftable 指向一个 MyInterface * 实际上是指向 QGraphicsLineItem vftable 的指针。

Because you're using multiple inheritance, the vftable pointer to what is expected to be a MyInterface* is actually a pointer to a QGraphicsLineItem vftable.

A dynamic_cast 会解决这个问题,因为它会返回正确的 vftable

A dynamic_cast would solve the issue because it will return the correct vftable

MyInterface* mInterface = dynamic_cast<MyInterface*>(item);

一个简单的例子:

class A
{
public:
    virtual void foo() = 0;
};

class B
{
public:
    virtual void goo() {};
};

class C : public B, public A
{
public:
    virtual void foo() {}; 
};

//....

B* c = new C;                  // c  is at 0x00a97c78 
A* a = (A*)c;                  // a  is at 0x00a97c78 (vftable pointer of B) 
A* a1 = dynamic_cast<A*>(c);   // a1 is at 0x00a97c7c (vftable pointer of A)

这篇关于运行时检查失败#0 - ESP的值在函数调用中未正确保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆