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



  • 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?


  • Moderators

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


  • @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. 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.


  • @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.


  • Moderators

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



  • @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 ?


  • Moderators

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



  • @raven-worx

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


  • Moderators

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


  • Qt Champions 2016

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


Log in to reply
 

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