【问题标题】:Qt shows backtrace by exitQt 通过退出显示回溯
【发布时间】:2018-12-04 12:03:29
【问题描述】:

我正在编写一个扫雷克隆程序以与 qt 取得联系。
当我使用右上角的 X 退出 Programm 时,我得到以下信息。

*** 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 并只使用 MineField,它就可以正常工作。

如果我没有得到这个输出,实际上一切都按方面工作。

感谢您的帮助。

【问题讨论】:

  • 调用 addWidget 会更改添加的小部件的父级。在你的main函数结束时,mineWrapper首先被销毁,boxLayout会再次销毁它的子元素(即mineWrapper),导致双释放错误。
  • @Botje 你在做某事,但不正确。
  • 实际问题是您在堆栈上分配了子 QObject,父对象将在子对象上调用 delete,并使用堆栈地址是未定义的行为。所以分配所有的QObejct(除了堆上的mainWindow,框架就是这样设计的)
  • 感谢@Zlatomir 这工作。

标签: c++ qt pointers


【解决方案1】:

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

固定代码:

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();

}

【讨论】:

    猜你喜欢
    • 2016-12-30
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多