Results 1 to 5 of 5

Thread: QAbstractItemModel object memory management

  1. #1
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default QAbstractItemModel object memory management

    Who deletes the memory related to the argument of QSortFilterProxyModel::setSourceModel() function?

    I saw code that passed the address of an object allocated on heap as argument to this function, but could not understand who was freeing that memory.

    Any information would be appreciated.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QAbstractItemModel object memory management

    Quote Originally Posted by kaswinikumar View Post
    I saw code
    Would be nice to see that code. But in general QSortFilterProxyModel::setSourceModel() does not take the ownership. so probably the model was created with a parent object and thus the model gets deleted if the parent object gets deleted. Thats a feature of Qt.

    EDIT: From the docs of QObject:
    QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren().

  3. #3
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QAbstractItemModel object memory management

    Thanks Lykurg. Unfortunately I am not sure if I can post the exact code here... but there were repeated invocations of setSourceModel() function each time with a new heap based model object [each one created with out a parent specified] and the existing model object (set in the previous invocation of that function) is not cleaned up anywhere. Even Valgrind reported that as leak, but I just wanted to know if I was missing something. Looks like it is a valid leak and I should delete those objects explicitly.

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

    Default Re: QAbstractItemModel object memory management

    If there is no parent, then the model will not be deleted by itself. It would be best just to make it have a parent that gets deleted around at the same time when you don't need the model (usually there such a situation exists). Otherwise you have to explicitly delete the object yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QAbstractItemModel object memory management

    Thanks Wysota.

Similar Threads

  1. Memory management in Qt?
    By wookoon in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2010, 18:20
  2. Memory Management Reg
    By BalaQT in forum Newbie
    Replies: 10
    Last Post: 4th February 2010, 12:43
  3. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  4. Memory management questions (im new to Qt)
    By scarvenger in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 07:41
  5. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 07:21

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.