[Interest] Automatic deletion of children of QObject

Jan Kundrát jkt at flaska.net
Wed Jun 13 23:35:16 CEST 2012


Hi Jochen,

On 06/13/12 22:50, Jochen Becher wrote:
> Of course it is not a bad idea to access siblings in a destructor. Why
> should it? If destruction follows in a known and reliable way there is
> no reason to avoid it. I do it to check some post-conditions using
> Q_ASSERT() to ensure all resources were freed and that involves some
> checks in siblings.

QObject's children are destroyed in an order which is not specified,
hence it's a bad idea to access "siblings" in a QObject's destructor.

In certain circumstances, a solution which uses QPointer can be handy,
ie. when you just want to take an optional cleanup like "hey buddy, if
you're still here, I'd like to let you know that I'm gone so that you
don't count on me being around anymore". However, it has big drawbacks
-- because you cannot affect the order of destruction reliably, you
cannot test both ways and it can be very easy to suddenly rely on one
particular order.

> I wouldn't name another developer's solution a bad idea without knowing
> why the solution was chosen...

In this case, your solution is bad because you depend on an order of
things which are not declared to be stable, sorry.

Cheers,
Jan

-- 
Trojita, a fast e-mail client -- http://trojita.flaska.net/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120613/87023007/attachment.sig>


More information about the Interest mailing list