简体   繁体   English

删除从QWidget继承的对象,WA_DeleteOnClose分段错误

[英]Deleting object that inherits from QWidget, WA_DeleteOnClose segmentation fault

I'm using an object the inherits QWidget, and in order to know when it is closed, I've used setAttribute(Qt::WA_DeleteOnClose) , and connected 我使用的是一个继承QWidget的对象,为了知道何时关闭,我使用了setAttribute(Qt::WA_DeleteOnClose)并进行了连接

connect(myObj,SIGNAL(destroyed(QObject*)),this,SLOT(handleFinish()));

However, when the object is being deleted, I get munmap_chunk(): invalid pointer , and when I look at the address of the pointer, it is one of the data members of myObj, which is really not a pointer. 但是,当删除对象时,我得到munmap_chunk(): invalid pointer ,当我查看指针的地址时,它是myObj的数据成员之一,实际上不是指针。

I allocate myObj dynamically, so it is supposed to be on the heap - myObj = new myObj(); 我动态分配myObj,因此应该将其放在堆上myObj = new myObj(); The error comes at the end of myObj destructor, and I've checked that this is the first time the destructor is called (after looking at When setting the WA_DeleteOnClose attribute on a Qt MainWindow, the program crashes when deleting the ui pointer ). 该错误发生在myObj析构函数的末尾,并且我检查了这是第一次调用该析构函数(查看之后, 在Qt MainWindow上设置WA_DeleteOnClose属性时,删除ui指针时程序崩溃 )。

Any suggestions for dealing with it? 有什么建议可以解决吗?

By the time you receive the destroyed signal, the object is only a QObject - not a QWidget and definitely not of any derived type. 到您收到destroyed信号时,该对象只是QObject不是QWidget而且绝对不是任何派生类型。 You can only access the members and methods provided via QObject , not via any other type. 您只能访问通过QObject提供的成员和方法,而不能通过任何其他类型访问。

It seems that you wish to be notified when a widget is about to close: for that, install an event filter that intercepts QEvent::close on the widget. 似乎您希望在小部件即将关闭时收到通知:为此,在小部件上安装一个拦截QEvent::close的事件过滤器。 See also this answer and a discussion of why a closeEvent cannot be generally handled via a slot . 另请参closeEvent 答案有关为何通常无法通过slot处理closeEvent的讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM