Question

My program does not appear to be leaking so I am curious about this. If I have initialized a subclassed QObject with new and I did not give the object a parent, when is it being destroyed? It seems to go against the specification of C++ but maybe it's possible the qt meta compiler is doing something behind the scenes.

More specifically, I am using a QThread and using moveToThread on the object with no parent (the implications of threads forces no parents). It appears that object under the thread gets destroyed when the main program finishes.

Was it helpful?

Solution

Dynamically allocated data without lifetime management will "live" until the application is terminated.

If your concern is with your worker objects leaking, you can call deleteLater() on them once their work is finished, and since it is a slot, you can also connect signals to it.

OTHER TIPS

Well, I would answer differently. You shouldn't consider Qt to be a magic tool which changes for you general C++ design concepts. Indeed it can take care about QObject's which are children of any other QObject and their live time limited to parent's live time. But in general it's up to developer to decide when C++ objects created and then they die. It's really bad practice to relay on fact that all QObjects will die somewhen on application quit. Complex application with a lot of QObjects can experience heavy performance impact after certain amount of QObjects being populated.

Answering your question, any modern OS will drop all allocated areas of particular process as soon as it quits.. but having paths in application where you dont control objects you created will lead you in troubles anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top