PDA

View Full Version : Qdialog - free memory?



marc2050
1st June 2011, 21:29
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!

Santosh Reddy
1st June 2011, 22:45
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)

ChrisW67
2nd June 2011, 00:55
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:


QHBoxLayout *layout = new QHBoxLayout;
QSpinBox *myspinbox = new QSpinBox;
... a bunch of other stuff
layout->addWidget(mySpinBox);
... other stuff
setLayout(layout);

and either bunch of other stuff throws an exception then you may leak memory.

DanH
2nd June 2011, 04:54
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.