在 Qt MainWindow 上设置 WA_DeleteOnClose 属性时,程序在删除 ui 指针时崩溃

2022-01-12 00:00:00 qt4 qt segmentation-fault c++

我在 MainWindow 中设置了 WA_DeleteOnClose 小部件属性.

I have set the WA_DeleteOnClose widget attribute in a MainWindow.

setAttribute(Qt::WA_DeleteOnClose);

但是,每当我关闭该主窗口时,我都会在其析构函数中遇到段错误,该析构函数只有 delete ui;

However, whenever I close that main window, I get a segfault in its destructor, which only has delete ui;

简而言之,在 Creator 中创建了一个 Qt4 GUI 应用程序,将 setAttribute(Qt::WA_DeleteOnClose); 添加到构造函数,程序现在在退出时崩溃.

In a nutshell, created a Qt4 GUI Application in Creator, added the setAttribute(Qt::WA_DeleteOnClose); to constructor, program now crashes on exit.

推荐答案

你是第一次在析构函数中遇到段错误,还是第二次?请记住,您的主窗口析构函数应该只运行一次.也就是说,它应该运行 either 因为堆栈展开,或 因为 WA_DeleteOnClose,而不是两者.

Are you getting a segfault in its destructor the first time, or the second time? Remember that your main window destructor should run only once. That is to say that it should run either because of a stack unwind, or because of WA_DeleteOnClose, not both.

IIRC,Creator 会将主窗口放在 main() 的堆栈上.因此,当 main() 返回时,主窗口被销毁.

IIRC, Creator will put the main window on the stack of main(). Therefore, when main() returns the main window is destroyed.

相关文章