Results 1 to 4 of 4

Thread: Qdialog - free memory?

  1. #1
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default Qdialog - free memory?

    Hi.

    When I do things like
    QSpinBox *myspinbox1 = new QSpinBox;
    and a host of other allocation of "new" pointer space in a Qdialog widget to present some option box choice for users, do I need to also do a "delete" to free up the memory space before I do a "close" for my dialog widget? Or will they be automatically freed when I close the widget?

    Thanks!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qdialog - free memory?

    They will be automatically deleted (when parent wideget is deleted) as long as they are placed the parent widget. (i.e, either specify parent while creation or, add to the layout of the widget)

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qdialog - free memory?

    Get into the habit of always providing a parent when you create a QObject or QWidget on the heap and you should never have to worry about leaks from these sources. If you do this:
    Qt Code:
    1. QHBoxLayout *layout = new QHBoxLayout;
    2. QSpinBox *myspinbox = new QSpinBox;
    3. ... a bunch of other stuff
    4. layout->addWidget(mySpinBox);
    5. ... other stuff
    6. setLayout(layout);
    To copy to clipboard, switch view to plain text mode 
    and either bunch of other stuff throws an exception then you may leak memory.

  4. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Qdialog - free memory?

    Virtually all widgets accept a "parent" pointer on their constructors. Get into the habit of always specifying a parent (wich usually can be "this", though think a little about what should "own" what) and you've got 95% of storage management handled.

Similar Threads

  1. Free memory - QPixmap::grabWindow?
    By hakermania in forum Newbie
    Replies: 3
    Last Post: 18th April 2011, 16:10
  2. Free memory with QProcess
    By Luc4 in forum Qt Programming
    Replies: 3
    Last Post: 8th March 2010, 10:55
  3. Free memory
    By TomASS in forum Newbie
    Replies: 10
    Last Post: 15th February 2010, 13:50
  4. free up the memory used by QHash
    By vishal.chauhan in forum Qt Programming
    Replies: 8
    Last Post: 22nd June 2009, 19:13
  5. what is free store in C++ memory?
    By Masih in forum General Programming
    Replies: 6
    Last Post: 2nd July 2007, 22:25

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.