Memory management in Qt?

I am new to Qt and I am wondering about some basic things with memory management and object life. When do I need to delete and / or destroy my objects? Is any of this handled automatically?

In the example below, which of the objects that I create need to be deleted? What happens to the myOtherClass instance variable when myClass destroyed? What happens if I don’t delete (or destroy) objects at all? Will this be a memory problem?

Myclass.h

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

Myclass.cpp

 MyClass::MyClass() { myOtherClass = new MyOtherClass(); MyOtherClass myOtherClass2; QString myString = "Hello"; } 

As you can see, this is pretty newbie-simple stuff, but where can I find out about it in a simple way?

+83
c ++ object memory qt
Mar 22 '10 at 11:17
source share
3 answers

If you create your own hierarchy using QObject s, that is, you initialize all the newly created QObject parent,

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

then delete parent enough, because the parent destructor takes care of destroying the child . (He does this by emitting signals, so it is safe even when you delete the child manually in front of the parent.)

You can also remove the child first, the order does not matter. For example, where order matters documentation about object trees .

If your MyClass not a child of a QObject , you will have to use the simple C ++ way of doing things.

Also note that the parent-child QObject hierarchy is usually independent of the hierarchy of the hierarchy tree / C ++ class tree. This means that the assigned child does not have to be a direct subclass of its parent. Any (subclass) of QObject will suffice.

However, there may be some restrictions imposed by designers for other reasons; for example, in QWidget(QWidget* parent=0) , where the parent must be a different QWidget , due to, for example, visibility flags and because you are doing the basic layout in this way; but for the Qt hierarchy system as a whole, you can have any QObject as a parent.

+89
Mar 22 '10 at 11:33
source share

I would like to extend Debilsky's answer, indicating that the concept of ownership is very important in Qt. When class A takes ownership of class B, class B is deleted when class A is deleted. There are several situations where one object becomes the owner of another, and not just when you create the object and specify its parent.

For example:

 QVBoxLayout* layout = new QVBoxLayout; QPushButton someButton = new QPushButton; // No owner specified. layout->addWidget(someButton); // someButton still has no owner. QWidget* widget = new QWidget; widget->setLayout(layout); // someButton is "re-parented". // widget now owns someButton. 

Another example:

 QMainWindow* window = new QMainWindow; QWidget* widget = new QWidget; //widget has no owner window->setCentralWidget(widget); //widget is now owned by window. 

So, often check the documentation, it is usually indicated whether the method will affect the ownership of the object.

As indicated by Debilski, these rules apply ONLY to objects that come from QObject. If your class is not the result of a QObject, you will have to deal with this destruction yourself.

+41
Mar 22 '10 at 12:00
source share

A parent (a QObject or its derived class) has a list of pointers to its children (QObject / its derived). The parent will delete all objects in its child list until the parent is destroyed. You can use this QObject property so that child objects are automatically deleted when the parent is ever deleted. Communication can be established using the following code

 QObject* parent = new QObject(); QObject* child = new QObject(parent); delete parent;//all the child objects will get deleted when parent is deleted, child object which are deleted before the parent object is removed from the parent child list so those destructor will not get called once again. 

There is another way to manage memory in Qt using smartpointer. The following article describes various smart pointers in Qt. https://blog.qt.digia.com/blog/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have/

+7
Mar 22 '10 at 11:55
source share



All Articles