Results 1 to 8 of 8

Thread: Memory management in Qt?

  1. #1
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Memory management in Qt?

    I'm quite new to Qt and am wondering on some basic stuff with memory management and the life of objects. When do I need to delete / destroy my objects? Is any of this handled automatically?

    In the example below, which of the objects I create do I need to delete? What happens to the instance variable myOtherClass when myClass is destroyed? What happens if I don't delete / destroy my objects at all, will that be a problem to memory?

    in MyClass.h:
    Qt Code:
    1. class MyClass
    2. {
    3.  
    4. public:
    5. MyClass();
    6. ~MyClass();
    7. MyOtherClass *myOtherClass;
    8. };
    To copy to clipboard, switch view to plain text mode 

    in MyClass.cpp:

    Qt Code:
    1. MyClass::MyClass() {
    2. myOtherClass = new MyOtherClass();
    3.  
    4. MyOtherClass myOtherClass2;
    5.  
    6. QString myString = "Hello";
    7. }
    To copy to clipboard, switch view to plain text mode 

    As you can see this is quite newbie-easy stuff but where can I learn about this in an easy way?

    Thanks really much
    Last edited by wysota; 14th September 2010 at 21:11.

  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: Memory management in Qt?

    Please scroll down and see the "Similar Threads" section. And next time please search the board prior to asking a question.
    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 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: Memory management in Qt?

    EDIT: too late...

  4. #4
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt?

    Hi all,

    About dynamic memory allocation :

    I had understood that in c++, any object created using the "new" operator should be destroyed by the "delete" operator.

    In the examples provided in Qt documentation, this does not seem to be the case. For instance, a QPushButton might be created by
    Qt Code:
    1. QPushButton *OKbutton = new QPushButton("OK");
    To copy to clipboard, switch view to plain text mode 
    and never destroyed afterwards.

    Is this specific to Qt ?
    Is there any tool available for linux to detect memory leaks ?


    Thanks
    Last edited by wysota; 6th November 2010 at 09:12.

  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: Memory management in Qt?

    Yes, this is specific to Qt. You can detect memory leaks using tools such as Valgrind.
    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. #6
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt?

    Thanks Wysota,
    So I guess the next question would be : at what point is the memory freed ?

    Is the QPushButton freed when the parent widget is destroyed, or when the program is closed ?
    In the first case, it complicates things, because in some cases it is required that the objects which have been created continue to exist when the dialog form is closed.
    In the latter case, the memory usage would be increasing progressively until the program session is ended, and in extreme cases it might be an issue.

    I couldn't find anything in the qt documentation explaining these matters. Does someone know of a link ?

  7. #7
    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: Memory management in Qt?

    Quote Originally Posted by Windsoarer View Post
    So I guess the next question would be : at what point is the memory freed ?

    Is the QPushButton freed when the parent widget is destroyed, or when the program is closed ?
    When the parent is destroyed.

    In the first case, it complicates things, because in some cases it is required that the objects which have been created continue to exist when the dialog form is closed.
    Then you simply don't make them children of the dialog.

    I couldn't find anything in the qt documentation explaining these matters. Does someone know of a link ?
    I can't find anything in the latest documentation since Nokia changed its layout. I had to go through the previous release to find it but here is the link: Object Trees.
    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.


  8. #8
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Memory management in Qt?

    Thnaks again

Similar Threads

  1. QEvent memory management
    By staser12 in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2010, 08:41
  2. QStackedWidget and memory management
    By martinn in forum Newbie
    Replies: 14
    Last Post: 8th April 2010, 00:14
  3. Memory Management Reg
    By BalaQT in forum Newbie
    Replies: 10
    Last Post: 4th February 2010, 12:43
  4. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  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.