[Interest] Automatic deletion of children of QObject

Jochen Becher jochen_becher at gmx.de
Tue Jun 12 21:36:46 CEST 2012


In my case childB is not a child of childA but both are children of
parent (widgets in the same group/layout). But childB has a reference to
childA which might be accessed in destructor. So I have either:

1) delete them in correct order (and I wouldn't like to create them in
unnatural oder just to enfore deletion in correct reverse order)

2) remove reference between objects before deleting them - I don't like
this way because parent's destructor has to know about all references
between child in that case.

I fixed it using my own QList of children which are deleted in reverse
order in parent's destructor...

Regards, Jochen


Am Dienstag, den 12.06.2012, 11:49 -0700 schrieb Jason H:
> Yes, interally, IIRC it is a QList, and suffer/enjoys all the traits
> of such.
> And FWIW, I think deleting children inthe order they were added makes
> the most sense. If you need your backwards behavior you are really
> implying that B is a child of A, and if created as such, you would get
> the behavior you describe...
> 
> 
> 
> 
> ______________________________________________________________________
> From: Andreas Pakulat <apaku at gmx.de>
> To: Jochen Becher <jochen_becher at gmx.de> 
> Cc: interest at qt-project.org 
> Sent: Tuesday, June 12, 2012 2:45 PM
> Subject: Re: [Interest] Automatic deletion of children of QObject
> 
> 
> Hi,
> 
> On Tue, Jun 12, 2012 at 8:37 PM, Jochen Becher <jochen_becher at gmx.de>
> wrote:
>         Hi,
>         
>         Think of the following code:
>         
>         
>         QObject *parent = new QObject();
>         QObject *childA = new QObject(parent);
>         QObject *childB = new QObject)parent);
>         
>         delete parent;
>         
>         
>         In which order would you expect childA and childB gets
>         deleted? I would
>         expect childB first and childA second. This would be same
>         behavior as in
>         using normal variables:
>         
>         QObject parent;
>         QObject childA(&parent);
>         QObject childB(&parent);
>         
>         Here, on leaving the scope childB would be deleted first,
>         childA second
>         and parent last.
>         
>         Isn't it a bug in Qt to delete children in the same order
>         there were
>         added to a parent instead of the reverse order?
> 
> 
> No, Qt makes no such guarantees. In fact it doesn't guarantee any
> specific order of destruction for the children, if you rely on any
> order here, your code is buggy.
> 
> 
> Its also not possible to actually do this, since you can add/remove
> childs from the list during the runtime of the program and the QObject
> cannot know wether the last-added object is really the one to be
> destroyed first (in your ideal scenario) or wether its maybe the one
> created first but was removed and re-added later.
> 
> 
> Since QObject keeps an ordered list of its children, I think its
> reasonable that they simply run over it from beginning to end and
> destroy the childs instead of going through the extra hoop of running
> from end to beginning.
> 
> 
> Andreas
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
> 
> 
> 





More information about the Interest mailing list