[Qt-interest] seg faults in QApplication destructor?

Thiago Macieira thiago.macieira at trolltech.com
Thu Jan 8 23:21:30 CET 2009


Alan Ezust wrote:
>Hrm... Come to think of it, you are right. When you delete a QObject,
>it should be removed from the child list of whatever parent it was in
>before... So forget that explanation.
>
>I'm trying to remember how I once had a segfault on exit, and what was
>the cause. It was related to out of order destruction of some sort,
>and changing the parent of a particular object made the problem go
>away...  Because it was not supposed to be destroyed at that time

The following two code blocks will cause crashes:

{
	QObject child;
	QObject parent;
	child.setParent(&parent);
}

{
	QObject *parent = new QObject;
	QObject *child = new QObject(&parent);
	delete parent;
	delete child;
}

The following two, however, work:
{
	QObject parent;
	QObject child;
	child.setParent(&parent);
	// or QObject child(&parent);
}

{
	QObject *parent = new QObject;
	QObject *child = new QObject(&parent);
	delete child;
	delete parent;
	// note the deletion order is the inverse from before
}
-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Software Engineer - Nokia, Qt Software
  Qt Software is hiring - ask me
      Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090108/819efc52/attachment.bin 


More information about the Qt-interest-old mailing list