PDA

View Full Version : Memory management in Qt?



wookoon
14th September 2010, 22:05
I'm quite new to Qt and am wondering on some basic stuff with memory management and the life of objects. When do I need to delete / destroy my objects? Is any of this handled automatically?

In the example below, which of the objects I create do I need to delete? What happens to the instance variable myOtherClass when myClass is destroyed? What happens if I don't delete / destroy my objects at all, will that be a problem to memory?

in MyClass.h:


class MyClass
{

public:
MyClass();
~MyClass();
MyOtherClass *myOtherClass;
};


in MyClass.cpp:



MyClass::MyClass() {
myOtherClass = new MyOtherClass();

MyOtherClass myOtherClass2;

QString myString = "Hello";
}


As you can see this is quite newbie-easy stuff but where can I learn about this in an easy way?

Thanks really much

wysota
14th September 2010, 22:11
Please scroll down and see the "Similar Threads" section. And next time please search the board prior to asking a question.

Lykurg
14th September 2010, 22:11
EDIT: too late...

Windsoarer
6th November 2010, 09:47
Hi all,

About dynamic memory allocation :

I had understood that in c++, any object created using the "new" operator should be destroyed by the "delete" operator.

In the examples provided in Qt documentation, this does not seem to be the case. For instance, a QPushButton might be created by

QPushButton *OKbutton = new QPushButton("OK");
and never destroyed afterwards.

Is this specific to Qt ?
Is there any tool available for linux to detect memory leaks ?


Thanks

wysota
6th November 2010, 10:12
Yes, this is specific to Qt. You can detect memory leaks using tools such as Valgrind.

Windsoarer
6th November 2010, 10:30
Thanks Wysota,
So I guess the next question would be : at what point is the memory freed ?

Is the QPushButton freed when the parent widget is destroyed, or when the program is closed ?
In the first case, it complicates things, because in some cases it is required that the objects which have been created continue to exist when the dialog form is closed.
In the latter case, the memory usage would be increasing progressively until the program session is ended, and in extreme cases it might be an issue.

I couldn't find anything in the qt documentation explaining these matters. Does someone know of a link ?

wysota
6th November 2010, 13:15
So I guess the next question would be : at what point is the memory freed ?

Is the QPushButton freed when the parent widget is destroyed, or when the program is closed ?
When the parent is destroyed.


In the first case, it complicates things, because in some cases it is required that the objects which have been created continue to exist when the dialog form is closed.
Then you simply don't make them children of the dialog.


I couldn't find anything in the qt documentation explaining these matters. Does someone know of a link ?
I can't find anything in the latest documentation since Nokia changed its layout. I had to go through the previous release to find it but here is the link: Object Trees.

Windsoarer
6th November 2010, 19:20
Thnaks again