Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Posts: 15
Registered: ‎07-15-2013
My Device: Blackberry Z10
My Carrier: Verizon

When I delete the parent of a qobject, will the qobject be deleted even if it was allocated to the heap?

For instance, if I am something list this:


socket = new QSslSocket(this); 


where this is a socket wrapper. If I explicitly delete the socket wrapper, will it also delete the QSslSocket object or do I need to do it myself?


Also, is there an accepted way of stopping and deleting threads. When I emit a signal to the quit slot, is it eventually going to emit a finished signal? Is it better to delete the thread or the worker I moved on to it first?



Posts: 170
Registered: ‎09-06-2010
My Device: None
My Carrier: None

Re: When I delete the parent of a qobject, will the qobject be deleted even if it was allocated to the heap?

Essentially yes. If you do the following:


QObject* parent = new QObject();
QObject* child = new QObject(parent);
delete parent;

 there is a relationship established between the parent and the child instances. In this example parent and child are both instances of QObject but the same relationship is established if the classes derive from QObject. The parent instance contains a list of pointers to its child object instances.


When you delete the parent, the child object instances each have their destructors called automatically where they free up their own resources. The key point is that you need to ensure that any classes you use, or create yourself derive, from QObject for this to work. If they don't you need to take care of cleaning up yourself.


Regarding threads and the correct way to use them in Qt. There is a quite a bit of "opinion" on how you should do this properly due to some poor Qt documentation in the past on overriding the run() method. There is a good discussion of this here:




which covers the topic as well as providing links to the some of the original articles that clarified the preferred way to do it.