运行时检查失败 #0 - ESP 的值未在函数调用中正确保存
我创建了一个简单的程序,演示了使用多重继承的 Qt 应用程序遇到的运行时错误。继承树如下所示:
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 中进行调试会导致运行时错误:
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.
知道问题是什么吗?
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
And here is the code:
/* 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;
}
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.
Any idea what the problem is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是多重继承,因此指向
MyInterface*
的vftable
指针实际上是指向QGraphicsLineItem vftable
的指针。dynamic_cast
可以解决这个问题,因为它将返回正确的vftable
一个简单的例子:
Because you're using multiple inheritance, the
vftable
pointer to what is expected to be aMyInterface*
is actually a pointer to aQGraphicsLineItem vftable
.A
dynamic_cast
would solve the issue because it will return the correctvftable
A simple example:
如果您使用动态转换
这个问题涉及各种 C++ 类型转换以及何时使用它们。在您的情况下,由于多重继承,您应该使用动态强制转换
You issue will be fixed if you use a dynamic cast
This questions deals various C++ casts and when to use them. In your case because of multiple inheritance you should be using dynamic casts