[Qt-interest] Maintaining strong references (shared pointer) to Qt objects

Anatoli Steinmark derletztemohikaner at gmx.de
Fri Jul 23 20:34:43 CEST 2010


Hi,

I'm trying to maintain strong references to Qt objects in my application.

As far as I understand Qt's memory management model, it maintains object trees with parent-child relationships. Setting a parent for a heap allocated widget means transferring the ownership to the parent.

Given the following code snippet:

// ...
QWidget bar;
QLabel* foo = new QLabel(&bar);
// ...

'bar' now owns 'foo' and is responsible for deleting it. Now I know that the following is valid:

// ...
QWidget bar;
QLabel* foo = new QLabel(&bar);
delete foo;
// ...

Because in 'foo's destructor it removes itself from its parent's to-delete list.

But the problem is that - given a parent's child was not deleted - whenever the parent is destroyed it destroys its children and any external references to the children in the application become dangling pointers.

So what I need is something that tells the parent that there are still external references to the children, so it should not delete them. Since widgets don't inherit from QSharedData, I have no idea how to do this.

Not setting parents is not an option, because in that case, widgets don't appear inside other widgets, since there is no relation between them.

Please help me to find a solution to this.

Thanks in advance!
-- 
Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief!  
Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail



More information about the Qt-interest-old mailing list