本文介绍了QGraphicsScene::~QGraphicsScene() 分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

对于 Qt 4.7.3,下面的示例在 QGraphicsScene::~QGraphicsScene() 调用时崩溃:

With Qt 4.7.3 an example below crashes at QGraphicsScene::~QGraphicsScene() call:

#include <QCoreApplication>
#include <QGraphicsScene>

int main( int argc, char* argv[] )
{
    // replace this with QObject app; and no problems
    QCoreApplication app( argc, argv );

    new QGraphicsScene( &app );

    return 0;
}

有什么想法吗?

更新:

错误报告已创建.

推荐答案

当构造一个 QGraphicsScene 实例时,它会将自身附加到一个列表中,该列表存储在单个 QApplication 实例,当它被删除时,它也会从该列表中删除自己:

When a QGraphicsScene instance is constructed it appends itself in a list stored in a private member of the single QApplication instance, and when it is deleted, it also remove itself from that list:

QGraphicsScene::~QGraphicsScene()
{
    Q_D(QGraphicsScene);

    // Remove this scene from qApp's global scene list.
    qApp->d_func()->scene_list.removeAll(this);

    ...
}

当应用对象被销毁时,继承的基类的析构函数被递归调用,所以,~QApplication()调用~QCoreApplication()~QCoreApplication()本身又调用~QObject().

When the application object is destroyed, the inherited base class' destructors are called recursively, so, ~QApplication() calls ~QCoreApplication() which itself calls ~QObject().

子对象的实际删除是在~QObject()中完成的.
这意味着在场景对象被销毁的时候,所有的QApplication成员都已经被销毁了,所以~QGraphicsScene()在尝试访问列表时会崩溃.

The actual deletion of child objects is done in ~QObject().
Which means that at the time the scene object is destroyed, all the QApplication members are already destroyed, so ~QGraphicsScene() crashes when it tries to access the list.

这篇关于QGraphicsScene::~QGraphicsScene() 分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 07:19