Let's break this silence

Erik esigra at gmail.com
Mon Aug 23 06:12:52 CEST 2010


  2010-08-22 21:54, Alexander Neundorf skrev:
>  Also all QObjects should be allocated on the heap, since they are 
delete'd
>  automatically by their parents.

...but not necessarily each object in a separate memory block. A QObject 
can be a member of its parent, or a member of the same struct/class that 
the parent is a member of (coming after the parent of course). In both 
cases, the order of destruction in C++ (or any object oriented language) 
ensures that the object will be destructed before its parent. (Then it 
will be unlinked from the parent, and the former parent's destructor 
will not attempt to delete the object.)

Actually, any way to allocate a QObject will work, even stack 
allocation, as long as it ensures that it will be destroyed before its 
parent.

After all, the only part of a QObject that assumes that its child is 
allocated as a single memory block on the heap is the destructor. So as 
long as the child is already destructed when its parent is destructed, 
all will be fine.

This applies to object types in many toolkits, not just QObject.


More information about the Kde-optimize mailing list