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

Malyushytsky, Alex alex at wai.com
Sat Jul 24 01:03:06 CEST 2010


I don't understand a reason widget should not be deleted.
What is going to happen to it? Persist until application exits?

It does not mean you can't prevent widget to be deleted by parent.
For example you can subclass parent and keep the least of the widget you don't want to be deleted.
Re-parenting them to NULL in the parent destructor should do it.

But whole approach seems not right.
I would stay with one parent per widget.
Memory should not be a concern - correctly designed widget should not require more memory than required for visualizing the data.
So if you need to have the same widget in 2 different places, you instantiate 2 objects.

If it still keeps the references to external objects in the widget, release them when widget is deleted.
If you reference this widget in external code, notify such code that it is going to be deleted.

By the way the only place you normally should see allocation widget on the stack is either in the main.cpp when main application widget is created or in the functions which create modal dialogs.
There might be exceptions, but if doing such thing in other place is a bit illogical and might lead to problems.

Regards,
     Alex


-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Anatoli Steinmark
Sent: Friday, July 23, 2010 11:35 AM
To: Qt-interest at trolltech.com
Subject: [Qt-interest] Maintaining strong references (shared pointer) to Qt objects

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
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”




More information about the Qt-interest-old mailing list