Results 1 to 11 of 11

Thread: Qt memory handling

  1. #1
    Join Date
    Jun 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Qt memory handling

    Hi,

    Since I've been using Qt, there has always been 1 big question on my mind and it's about how Qt handles new/deletes. The biggest problem I have is that the Qt-help gives you a lot of information you don't need, but doesn't give you the answers you are looking for (to be honest, I think that this is a big mistake of the help-builder guys).

    For instance, I'm using a QTabWidget and I'm dynamically inserting tab pages. Each tab page needs his own widget. Nowhere in the help I can find who deletes this widget. Is Qt deleting this widget after destruction, or am I responsible for it ? In this case it's with a QTabWidget, but I have this problem with a lot of other Qt objects. The help files give you no clue at all on how to handle these kind of allocations. Is there any logic on how to know when I need to delete a object, or when Qt is deleting it for me ?

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt memory handling

    Quote Originally Posted by Skizmo View Post
    ...
    For instance, I'm using a QTabWidget and I'm dynamically inserting tab pages. Each tab page needs his own widget. Nowhere in the help I can find who deletes this widget. Is Qt deleting this widget after destruction, or am I responsible for it ? In this case it's with a QTabWidget, but I have this problem with a lot of other Qt objects. The help files give you no clue at all on how to handle these kind of allocations. Is there any logic on how to know when I need to delete a object, or when Qt is deleting it for me ?
    I will disagree with you. Qt help contains a part called "Object Trees and Object Ownership" which says:

    "Construction/Destruction Order of QObjects
    When QObjects are created on the heap (i.e., created with new), a tree can be constructed from them in any order, and later, the objects in the tree can be destroyed in any order. When any QObject in the tree is deleted, if the object has a parent, the destructor automatically removes the object from its parent. If the object has children, the destructor automatically deletes each child. No QObject is deleted twice, regardless of the order of destruction."

    This answers your question about QTabWidget and your pages.
    I'm a rebel in the S.D.G.

  3. The following user says thank you to lyuts for this useful post:

    Skizmo (17th November 2008)

  4. #3
    Join Date
    Jun 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt memory handling

    Ah !

    Thanx, apparently I didn't dig enough into the help files. I'm going to look into it

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt memory handling

    Yes, but the more interesting question is: if you create a QWidget (let's say: unparented) with new and then insert it to a QTabWidget with QTabWidget::insertTab(), will the QTabWidget take ownership?

    (In all probability it will, but still I like it if a function that gets passed a pointer, explicitly states that/if it will take ownership. In this case the docs do not make a statement which might leave you wonder - esp. if you are not that experienced with Qt.)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt memory handling

    Quote Originally Posted by caduel View Post
    Yes, but the more interesting question is: if you create a QWidget (let's say: unparented) with new and then insert it to a QTabWidget with QTabWidget::insertTab(), will the QTabWidget take ownership?
    The answer is logically trivial. A widget occupies part of its parent's rectangle. Hence if you see the widget inside the tab widget, it has to be owned by it. Hence it gets destroyed when the tab widget is deleted. There are no exceptions to this rule. Note that if you remove the tab from the tab widget before deleting the tab widget, the tab will not get deleted.

  7. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt memory handling

    Here is the answer to your question:

    Qt Code:
    1. tabWidget = new QTabWidget(this);
    2.  
    3. btn = new QPushButton;
    4.  
    5. tabWidget->addTab(btn, tr("newTab"));
    6.  
    7. qDebug() << tabWidget;
    8. qDebug() << btn->parent()->parent();
    To copy to clipboard, switch view to plain text mode 

    prints this
    Qt Code:
    1. QTabWidget(0x51a33a0)
    2. QTabWidget(0x51a33a0)
    To copy to clipboard, switch view to plain text mode 

    btn's parent is QStackedWidget, while the QStackedWidget's parent is tabWidget.
    Last edited by lyuts; 17th November 2008 at 13:14.
    I'm a rebel in the S.D.G.

  8. #7
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt memory handling

    Another question from that category (I already asked it in this forum but did not get a response) is who is responsible for deleting instances that QNetworkManager passes to the client with "readytoread" or smth (I don' remember syntax exactly). In short, the slot receives the pointer to an instance that is used to retrieve the data. Who is responsible for deleting this allocation?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt memory handling

    The parent As homework, the check of who is the parent of QNetworkReply is left to you. By the way, remember you can always explicitly delete an object yourself, regardless of who is its parent. Just don't do that from within a slot (if you have to, use QObject::deleteLater()).

  10. The following user says thank you to wysota for this useful post:

    QPlace (18th November 2008)

  11. #9
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt memory handling

    Maybe you can delete QWidgets.
    QObjects ... not always. E.g. the model in a QAbstractItemView is (or maybe was) held by a raw pointer and thus the view does (or did) not realize if you destroyed the model.
    (I suggested to the trolls that this should be 'fixed' as we are spoilt by Qt to usually be able to destroy QObjects.)

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt memory handling

    I'm not sure you are right. The fact that there is a regular pointer doesn't yet mean there is something wrong. A QObject emits a signal before getting destroyed, so the view can still react on the object being deleted. If it's not implemented, you can do that yourself

  13. #11
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt memory handling

    That is correct, of course. A raw pointer does not implicate any trouble by itself.

    Checking my emails I must correct myself (a bit): the issue was destroying a sourcemodel of a QAbstractItemProxyModel.
    The view does notice when it's model is deleted, the proxy, however, does (or did) not.
    I deleted the old model and got a crash.
    (Since then I am a bit more careful what QObjects I may delete while they are in use somewhere (by Qt).)

Similar Threads

  1. Replies: 0
    Last Post: 26th September 2008, 21:33
  2. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  3. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  4. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  5. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.