[Interest] Automatic deletion of children of QObject

André Pönitz andre.poenitz at mathematik.tu-chemnitz.de
Wed Jun 13 23:56:46 CEST 2012


On Wed, Jun 13, 2012 at 11:35:16PM +0200, Jan Kundrát wrote:
> 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.

If deterministic order of destruction is needed one can always do it
"manually" in the destructor body of the QObject-derived parent class
instead of waiting for the parent's base class desctructor with
it's "non-deterministic order of destruction" kicks in.

Andre'



More information about the Interest mailing list