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. Does "take ownership" also mean take responsibility for delete?
Forum Update on Monday, May 27th 2025

Does "take ownership" also mean take responsibility for delete?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.8k Views
  • 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.
  • H Offline
    H Offline
    Harry123
    wrote on last edited by Harry123
    #1

    There are various functions that take ownership of an object.

    Some examples are QMenu::addAction(QAction pointer) or QMainWindow::addDockWidget(QDockWidget pointer), where the pointers were obtained on the heap by the new operator.

    Does this mean that after the call to QMenu::addAction there is no need to call delete on the pointer, or will not calling delete cause a memory leak?

    raven-worxR 1 Reply Last reply
    0
    • H Harry123

      There are various functions that take ownership of an object.

      Some examples are QMenu::addAction(QAction pointer) or QMainWindow::addDockWidget(QDockWidget pointer), where the pointers were obtained on the heap by the new operator.

      Does this mean that after the call to QMenu::addAction there is no need to call delete on the pointer, or will not calling delete cause a memory leak?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Harry123

      1. ownership = taking care of delete
      2. delete will never cause a leak ;)
      3. delete may produce a crash when delete is called again on the same pointer (without setting it to NULL)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      H 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @Harry123

        1. ownership = taking care of delete
        2. delete will never cause a leak ;)
        3. delete may produce a crash when delete is called again on the same pointer (without setting it to NULL)
        H Offline
        H Offline
        Harry123
        wrote on last edited by
        #3

        @raven-worx

        Thanks for the answer. Does that mean that :

        1. After doing QMenu::addAction(pointer) I can forget about the pointer and have no memory leak
        2. After doing QMenu::addAction(pointer) I can delete the pointer and not cause a crash in QMenu
        1 Reply Last reply
        0
        • T Offline
          T Offline
          tomma
          wrote on last edited by tomma
          #4
          1. QWidget::addAction(QAction *action) does NOT take ownership of action!
            QMenu::addAction methods which create and return QAction* take ownership of action.
          2. Deleting QAction which is shown in menu (owned or not) should be safe as long as it isn't done in said actions event handling and in that case deleteLater() should be used.
          H 1 Reply Last reply
          0
          • T tomma
            1. QWidget::addAction(QAction *action) does NOT take ownership of action!
              QMenu::addAction methods which create and return QAction* take ownership of action.
            2. Deleting QAction which is shown in menu (owned or not) should be safe as long as it isn't done in said actions event handling and in that case deleteLater() should be used.
            H Offline
            H Offline
            Harry123
            wrote on last edited by Harry123
            #5

            @tomma

            There an area of uncertainty in the Qt documentation for all "add(pointer)" functions as regarding ownership.

            Regarding QMenu::addAction(QAction*), it exists but is not found in the documentation for QMenu.
            The documentation for QMainWindow::addDockWidget(...,pointer) does not mention ownership at all.

            Should I take it that when taking ownership is NOT mentioned in the documentation, then ownership is not taken ?
            And does ownership mean that there is no need to delete the pointer?

            Could somebody please give here a straight and simple answer.

            raven-worxR 1 Reply Last reply
            0
            • H Harry123

              @tomma

              There an area of uncertainty in the Qt documentation for all "add(pointer)" functions as regarding ownership.

              Regarding QMenu::addAction(QAction*), it exists but is not found in the documentation for QMenu.
              The documentation for QMainWindow::addDockWidget(...,pointer) does not mention ownership at all.

              Should I take it that when taking ownership is NOT mentioned in the documentation, then ownership is not taken ?
              And does ownership mean that there is no need to delete the pointer?

              Could somebody please give here a straight and simple answer.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @Harry123 said:

              Should I take it that when taking ownership is NOT mentioned in the documentation, then ownership is not taken ?

              generally yes.
              You can expect ownership transfer for all widgets which get reparented.
              E.g. when adding a widget to a layout for example.
              Also every time you set a parent to QObject you do not need to take care of the deletion.

              And does ownership mean that there is no need to delete the pointer?

              see 1) from my post before
              The one who has ownership takes care of the deletion - as the term "ownership" already implies.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              H 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @Harry123 said:

                Should I take it that when taking ownership is NOT mentioned in the documentation, then ownership is not taken ?

                generally yes.
                You can expect ownership transfer for all widgets which get reparented.
                E.g. when adding a widget to a layout for example.
                Also every time you set a parent to QObject you do not need to take care of the deletion.

                And does ownership mean that there is no need to delete the pointer?

                see 1) from my post before
                The one who has ownership takes care of the deletion - as the term "ownership" already implies.

                H Offline
                H Offline
                Harry123
                wrote on last edited by
                #7

                @raven-worx

                Thanks for the answer.

                Just one more point to be sure : When doing "add(pointer)" with reparenting, will leaving the local pointer variable dangling cause any memory leak problems ?

                raven-worxR 1 Reply Last reply
                0
                • H Harry123

                  @raven-worx

                  Thanks for the answer.

                  Just one more point to be sure : When doing "add(pointer)" with reparenting, will leaving the local pointer variable dangling cause any memory leak problems ?

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @Harry123
                  no. You should read what a pointer actually is.
                  A pointer is just a variable holding a address to memory.
                  So the pointer variable will be deleted, but not the data it points to.

                  deletewill swipe the data in memory where the pointer points to.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  H 1 Reply Last reply
                  0
                  • raven-worxR raven-worx

                    @Harry123
                    no. You should read what a pointer actually is.
                    A pointer is just a variable holding a address to memory.
                    So the pointer variable will be deleted, but not the data it points to.

                    deletewill swipe the data in memory where the pointer points to.

                    H Offline
                    H Offline
                    Harry123
                    wrote on last edited by
                    #9

                    @raven-worx

                    I was worried about (too) smart pointers.
                    Thanks again for your answers.

                    raven-worxR 1 Reply Last reply
                    0
                    • H Harry123

                      @raven-worx

                      I was worried about (too) smart pointers.
                      Thanks again for your answers.

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by raven-worx
                      #10

                      @Harry123
                      smart pointers are a different story. They delete themselves when the last reference (see "reference counting" if you are interested) to it is removed.
                      But you must never delete a smart pointer by yourself, thus you should not pass it to such a Qt method which will delete it in the end.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      kshegunovK 1 Reply Last reply
                      0
                      • raven-worxR raven-worx

                        @Harry123
                        smart pointers are a different story. They delete themselves when the last reference (see "reference counting" if you are interested) to it is removed.
                        But you must never delete a smart pointer by yourself, thus you should not pass it to such a Qt method which will delete it in the end.

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #11

                        @Harry123

                        @raven-worx said:

                        But you must never delete a smart pointer by yourself, thus you should not pass it to such a Qt method which will delete it in the end.

                        Except when one takes the ownership from the smart pointer class explicitly (if possible at all), e.g.: QScopedPointer::take().

                        Read and abide by the Qt Code of Conduct

                        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