Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management in Qt?

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 and/or 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 (or destroy) my objects at all? Will that be a problem to memory?

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 quite newbie-easy stuff but where can I learn about this in an easy way?

like image 249
Martin Avatar asked Mar 22 '10 11:03

Martin


People also ask

Does Qt have garbage collection?

Qt doesn't have garbage collection. It has a concept of ownership. With new QProcess(this); , you made the new QProcess object owned by this instance of MainWindow . When a QObject is destroyed, it in turn destroys all objects it owns.

Do I need to delete Qt objects?

Hi, Yes it's necessary, ui is allocated on the heap, so it must be deleted when not used anymore. Otherwise you would have a memory leak. OK ui is necessary.

How do I use Qpointer?

Use QPointers like normal pointers and you will not need to read this class documentation. For creating guarded pointers, you can construct or assign to them from a T* or from another guarded pointer of the same type. You can compare them with each other using operator==() and operator!


2 Answers

If you build your own hierarchy with QObjects, that is, you initialise all newly created QObjects with a parent,

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

then it is enough to delete the parent, because the parents destructor will take care of destroying child. (It does this by issuing signals, so it is safe even when you delete child manually before the parent.)

You could also delete the child first, the order doesn't matter. For an example where the order does matter here's the documentation about object trees.

If your MyClass is not a child of QObject, you’ll have to use the plain C++ way of doing things.

Also, note that the parent–child hierarchy of QObjects is generally independent of the hierarchy of the C++ class hierarchy/inheritance tree. That means, that an assigned child does not need to be a direct subclass of it’s parent. Any (subclass of) QObject will suffice.

There might be some constraints imposed by the constructors for other reasons, however; such as in QWidget(QWidget* parent=0), where the parent must be another QWidget, due to e.g. visibility flags and because you’d do some basic layout that way; but for Qt's hierarchy system in general, you are allowed to have any QObject as a parent.

like image 111
Debilski Avatar answered Sep 28 '22 05:09

Debilski


I'd like to extend Debilski's answer by pointing out that the concept of ownership is very important in Qt. When class A assumes 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, not just when you create an object and specify its parent.

For instance:

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, check the documentation often, it generally specifies whether a method will affect the ownership of an object.

As stated by Debilski, these rules apply ONLY to objects that derive from QObject. If your class does not derive from QObject, you'll have to handle the destruction yourself.

like image 41
Austin Avatar answered Sep 28 '22 04:09

Austin