Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Releasing memory
Forum Updated to NodeBB v4.3 + New Features

[Solved] Releasing memory

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ThaRez
    wrote on last edited by
    #1

    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
    Richard

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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).

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ThaRez
        wrote on last edited by
        #3

        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!
        -Richard

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Cheers.

          Some additional docs that may clarify the issue:

          • "Object trees":http://developer.qt.nokia.com/doc/qt-4.8/objecttrees.html
          • "Object model":http://developer.qt.nokia.com/doc/qt-4.8/object.html
          • "QObject":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html
          • "The Meta-Object System":http://developer.qt.nokia.com/doc/qt-4.8/metaobjects.html

          (Z(:^

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ThaRez
            wrote on last edited by
            #5

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

            QPixmap
            QSpacerItem
            QTime
            QDate

            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.
            -Richard

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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.

              (Z(:^

              1 Reply Last reply
              0
              • B Offline
                B Offline
                broadpeak
                wrote on last edited by
                #7

                QScopedPointer is another solution.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  ThaRez
                  wrote on last edited by
                  #8

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

                  • Richard
                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    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.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved