DO NOT delete QObjects, mmmkay?

David Faure faure at kde.org
Wed Nov 14 15:40:53 GMT 2007


On Thursday 08 November 2007, Andreas Hartmetz wrote:
> Hi all,
> 
> All in all I have fixed three or four crashes in various KDE modules where 
> QObjects were deleted while a signal/slot call involving that QObject was 
> going on.
> It's documented that you should only delete a QObject when you can rule out 
> that this might happen. These bugs do not trigger every time and they are a 
> great way to get buggy software in user's hands. Just say no to deleting 
> QObjects except if you *really* know what you're doing. Not seeing the danger 
> right away does not count as knowing what you're doing.

While I agree with you that one should be careful with deleting QObjects (in particular,
one should not delete a QObject from a slot, when that QObject is either the sender
of the signal or the receiver (i.e. this)) .... I think that over-using deleteLater makes
debugging much more difficult. E.g. if you access deleted memory, all that valgrind
will be able to tell you is "this object has been deleted from QCoreApplication::sendPostedEvents".
Adding a breakpoint on deleteLater() to see where it's called can take a long time since
it's called from many places...

Deleting QObjects with "delete" is fine when that object isn't inside a signal emission
or inside one of its slots - and there are many cases where one can be sure of that.

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).




More information about the kde-core-devel mailing list