C++ Qt通过退出显示回溯

C++ Qt通过退出显示回溯,c++,qt,pointers,C++,Qt,Pointers,我正在编写一个扫雷舰克隆,以便与qt取得联系。 当我退出程序时,在右角有一个X,我得到如下结果 *** Error in `/home/.test/build-minesweeper-Desktop-Profile/minesweeper': free(): invalid pointer: 0x00007ffc01eba100 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7fd2d0b

我正在编写一个扫雷舰克隆,以便与qt取得联系。
当我退出程序时,在右角有一个X,我得到如下结果

*** Error in `/home/.test/build-minesweeper-Desktop-Profile/minesweeper': free(): invalid pointer: 0x00007ffc01eba100 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7fd2d0b93bfb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76fc6)[0x7fd2d0b99fc6]
/lib/x86_64-linux-gnu/libc.so.6(+0x7780e)[0x7fd2d0b9a80e]
/usr/lib/x86_64-linux-   gnu/libQt5Core.so.5(_ZN14QObjectPrivate14deleteChildrenEv+0x71)[0x7fd2d1e96e11]
/usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5(_ZN7QWidgetD1Ev+0x36b)[0x7fd2d2792bdb]
/home/.test/build-minesweeper-Desktop-Profile/minesweeper(+0x4e2b)[0x55ef85688e2b]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fd2d0b432e1]
/home/.test/build-minesweeper-Desktop-Profile/minesweeper(+0x4f4a)[0x55ef85688f4a]
======= Memory map: ========
55ef85684000-55ef8568c000 r-xp 00000000 fe:05 335827230   /home/.test/build-minesweeper-Desktop-Profile/minesweeper
我没有包括完整的输出

这是我的主要功能

int main(int argc, char **argv) {

    QApplication app(argc, argv);

    QPushButton test;
    test.setText("test");

    QWidget mainWindow;
    QBoxLayout boxLayout(QBoxLayout::TopToBottom);
    QWidget mineWrapper;

    MineField layout;
    boxLayout.addWidget(&mineWrapper);
    boxLayout.addWidget(&test);

    mineWrapper.setLayout(&layout);
    mainWindow.setLayout(&boxLayout);

    mainWindow.setFixedSize(800, 600);
    mainWindow.show();

    return app.exec();

}
如果我删除Boxlayout,只使用雷区,效果会很好

实际上,如果我不想得到这个输出,那么一切都是按方面进行的


感谢您的帮助。

感谢@Zlatomir,我必须在堆上而不是堆栈上分配QObject

固定代码:

int main(int argc, char **argv) {

    QApplication app(argc, argv);

    QPushButton * test = new QPushButton;
    test->setText("test");

    QWidget mainWindow;
    QBoxLayout * boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
    QWidget *mineWrapper = new QWidget;

    MineField *layout = new MineField;
    boxLayout->addWidget(mineWrapper);
    boxLayout->addWidget(test);

    mineWrapper->setLayout(layout);
    mainWindow.setLayout(boxLayout);

    mainWindow.setFixedSize(800, 600);
    mainWindow.show();

    return app.exec();

}

调用
addWidget
更改所添加小部件的父级。在
main
函数的末尾,
mineWrapper
首先被销毁,
boxLayout
将再次销毁其子项(即
mineWrapper
),从而导致双重自由错误。@Botje,但不正确。实际问题是您在堆栈上分配了子QObject,父对象将在子对象上调用delete,而使用堆栈地址调用delete是未定义的行为。因此,分配所有QObejct(除了堆上的主窗口,这就是框架的设计工作方式),感谢@Zlatomir的工作。