[Interest] Create the main widget on heap in main()

Etienne Sandré-Chardonnal etienne.sandre at m4x.org
Sun Jan 12 19:04:53 CET 2014


> Fix the double-deletes instead - those are real issues! If an object is
> double-deleted it might be an indication that it should not have been
> allocated on the stack in the first place...
>

This was what I wanted to underline, maybe not clear from my side...
Allocating on the stack an object that has some destruction mechanisms
elsewhere is not a good idea IMHO, so I never allocate any QObject on
stack. And this is not only for objects with parent : any connection to
deleteLater() or flags such as WA_DeleteOnClose allows the object to be
deleted, and this may be invisible in the main(). For instance, if main()
and QMainWidget are written by different developers.

One of the most reliable Qt way of deleting QObjects remains (again IMHO):
connect(currentThread(), SIGNAL(finished()), object, SLOT(deleteLater()));

Because it cannot double delete and does not even require storing a
pointer. And will always delete unless the app crashes.

I've never used it on the main widget, though, but there is no reason it
should'nt work
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140112/cb35ae6f/attachment.html>


More information about the Interest mailing list