09-09-2013 03:29 PM
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?
09-11-2013 08:57 AM
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.