Results 1 to 7 of 7

Thread: Qt and C++ dynamic memory handling...

  1. #1
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Qt and C++ dynamic memory handling...

    Hi guys,

    i don't know what i missed, but can anyone explain me why all members of a QT class should be allocated on heap: i.e. QLabel *x = new QLabel

    Why not on the stack? I know that everything you allocate on the heap, doesn't get destroyed until you call explicit the delete command...

    Second, I know Qt deletes all child objects of a class, so i don't have to define the destructor, right?

    Thanks

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt and C++ dynamic memory handling...

    In fact, calling the destructor (declared as virtual) of a QObject calls the destructor for all children.

    So you allocate an object, add it as a child, and let the parent delete it for you, cause it owns it now.

    But the destructor always need to be defined since it will be executed at object hierarchy destruction or explicitly delete call.

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

    giowck (18th November 2009)

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt and C++ dynamic memory handling...

    If you put them on the stack its far too easy for the ownership to be wrong and then you'll have all sorts of memory problems.

    Normally, the parent deletes the object, so it needs to be allocated somewhere that will not attempt to delete it directly.

  5. The following user says thank you to squidge for this useful post:

    giowck (18th November 2009)

  6. #4
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: Qt and C++ dynamic memory handling...

    Quote Originally Posted by fatjuicymole View Post
    If you put them on the stack its far too easy for the ownership to be wrong and then you'll have all sorts of memory problems.
    thanks both for the answers...

    But i didn't understand what you mean with memory problems, what kind of problems? Any link/reference to this? I don't know this issue...

  7. #5
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Qt and C++ dynamic memory handling...

    Quote Originally Posted by giowck View Post
    Hi guys,
    Why not on the stack? I know that everything you allocate on the heap, doesn't get destroyed until you call explicit the delete command...
    Right. Thats the reason why you should always pass proper parents to your objects. Usually you create the root object in the main-method on the stack.

    Quote Originally Posted by giowck View Post
    Second, I know Qt deletes all child objects of a class, so i don't have to define the destructor, right?
    Wrong. Your destructor should release all resources you allocated yourself and that get not freed by Qt. It depends on your resources whether you need a destructor not on the fact that you are using Qt.
    It's nice to be important but it's more important to be nice.

  8. The following user says thank you to axeljaeger for this useful post:

    giowck (18th November 2009)

  9. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt and C++ dynamic memory handling...

    Quote Originally Posted by giowck View Post
    But i didn't understand what you mean with memory problems, what kind of problems? Any link/reference to this? I don't know this issue...
    Well, if you transfer the ownership of an object to another object (eg. using QLayout::addItem), then it's that objects responsibility to delete it. If you delete it too by for example, going out of scope, you have a double free error, which may corrupt the free list, dereference a null pointer, or whatever.

  10. #7
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt and C++ dynamic memory handling...

    ok
    i looked on QObject docs and i solved my doubts! Thank you guys

Similar Threads

  1. Changing memory allocation managment ( QT 4.4.3 )
    By Link130 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th October 2009, 09:44
  2. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  3. Dynamic memory release
    By linuxdev in forum Qt Programming
    Replies: 5
    Last Post: 11th December 2008, 06:22
  4. QPixmap memory handling
    By Angelo Moriconi in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 15:37
  5. Qt memory handling
    By Skizmo in forum Qt Programming
    Replies: 10
    Last Post: 18th November 2008, 11:07

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.