[Solved] Releasing memory

  • Hello
    I'm still a bit confused what memory I need to release. For instance:

    If I create a object from a class using "new" and this class both includes a "appearance" (widget with attached layouts with embedded buttons e.g.), as well as QTimer pointers that are initiated and created with new, and other custom objects created with new as well. When deleting this main object described above, what else do I need to delete? I noticed when trying to implement the deconstructor that I can't delete a QPushButton after I've deleted the layout to which this is attached. Therefore, I'm wondering how far this "automation" stretches, and which objects I'll have to delete myself.

    Thank you!
    Best regards

  • Moderators

    Qt uses it's superb Meta Object system to implement "garbage collector" - to an extent possible in C++. As you probably know, Qt objects need to be instantiated with a QObject *parent given. This creates a hierarchy of meta objects.

    When you delete a parent object, it automatically deletes all children. That should answer your question. The "automation" stretches as far as QObject hierarchy is preserved. All non-QtObjects remain unaffected, as well as QtObjects outside of hierarchy (or residing in different branch of the hierarchy).

  • OK, so as long as the child object initiated with new is given a parent (taken the structure is intact) it will be deleted along its parent. Great! Thank you!

  • Moderators

  • Thank again, think I got it mostly figured out. One final thing though, what about Qt object that do not take a parent like:


    Do I need to remove these manually? What about the QPixmap, if it's attached to a QLabel will it then be deleted along with the label? and the same thing to QSpacerItem, being attached to a layout.

  • Moderators

    Those are not QtObjects. For all that you create yourself, you need to delete them yourself, just as in "normal" C++. If you need more time to clean after some objects, you can take a look at "QObject::deleteLater()":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html#deleteLater

    I'm not sure about QLabel, I don't remember how pixmaps are handled there. Most probably you have to delete yourself. Read documentation on QLabel, and if there is no mention of this subject, assume that you have to delete yourself. If you are suicidal, you can also take a look into QLabel source code, but probably a quick test application is much better option.

  • QScopedPointer is another solution.

  • It now seems to be ok, without any memory leaks. Thank you for you assistance!

    • Richard

  • QSpacerItem is put into a layout (and usually not needed by user programs, use the addSpacing/addStretch methods and friends).

    QPixmap, QTime and QDate are usually used as stack based objects (not heap based using new), and as such don't need to be deleted manually.

    See my "comment":/forums/viewreply/69993/ on another thread about when to call new/delete etc.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.