Results 1 to 7 of 7

Thread: Destroy object

  1. #1
    Join Date
    Jul 2012
    Posts
    123
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Destroy object

    Hi,
    i programming in qt already sometime and allway i am worried about memory leaks. So this is my question:
    In classic Qt programming i used to use something like:
    Qt Code:
    1. MyObject *object = new MyObject(this);
    To copy to clipboard, switch view to plain text mode 
    . Is this correct? Will object be delete automatic or i created memory leak?

    Thanks

  2. #2
    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: Destroy object

    It depends what "this" and "MyObject" are. If they are both descendants of QObject then there is no memory leak however object will not be deleted until "this" is deleted.
    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.


  3. #3
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Destroy object

    this is correct but to be sure about not having memory leak just delete objects whenever they are no longer needed

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Destroy object

    to be sure about not having memory leak just delete objects whenever they are no longer needed
    This is not correct! As Wysota said, if an object descends from QObject, and is created with another QObject class as its parent, then the parent, not the user, will almost always be responsible for deleting the object. If you manually delete a QObject that has a parent, the destructor will be sure to remove it from the parent's list of children, but nevertheless, this is not good Qt practice. Let QObject handle the child lifetimes, as it was designed to do and you won't get into trouble.

    Ordinary C++ object instances (not derived from QObject) follow the normal C++ rules for creation and deletion, but QObject classes are different because of the parent-child relationship that is established when a QObject instance is created with a parent (or assigned one later).

    The major exception to this Qt rule is in the Graphics / View architecture: A QGraphicsItem instance that is added to a QGraphicsScene but then later removed from it through a call to QGraphicsScene::removeItem() must be manually deleted. However, instances that are permanently added to the scene do not have to be deleted, because the scene is in charge of their lifetimes and they will be deleted when the scene instance is deleted.

  5. #5
    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: Destroy object

    Quote Originally Posted by d_stranz View Post
    The major exception to this Qt rule is in the Graphics / View architecture: A QGraphicsItem instance that is added to a QGraphicsScene but then later removed from it through a call to QGraphicsScene::removeItem() must be manually deleted. However, instances that are permanently added to the scene do not have to be deleted, because the scene is in charge of their lifetimes and they will be deleted when the scene instance is deleted.
    This is really no exception. The same happens for Q{List,Table,Tree}WidgetItem instances and possibly everything else that loses its parentship (including QObject instances if you call setParent(0) on them).
    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.


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

    d_stranz (4th January 2013)

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Destroy object

    The same happens for Q{List,Table,Tree}WidgetItem instances
    Yes, true. I tend to overlook these cases. I guess I have rarely come across this in actual usage - most often this is hidden in the details of a model - view interaction, or, if I build a table manually, I just clear the former contents first then re-fill the whole thing. In my usage, QGraphicScene implementations tend to be more dynamic.

  8. #7
    Join Date
    Jul 2012
    Posts
    123
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Destroy object

    Thanks That's what i need

Similar Threads

  1. Check if a object is null and destroy objects
    By ruben.rodrigues in forum Newbie
    Replies: 3
    Last Post: 2nd July 2010, 10:25
  2. How to destroy an object right
    By Cruz in forum Newbie
    Replies: 7
    Last Post: 22nd January 2009, 19:43
  3. Object Not destroy
    By Sudhanshu Sharma in forum Qt Programming
    Replies: 3
    Last Post: 30th July 2008, 23:16
  4. How to destroy a thread ?
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2006, 18:04
  5. Replies: 1
    Last Post: 24th June 2006, 20:55

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.