PDA

View Full Version : Memory Leaks



kaushal_gaurav
20th October 2008, 11:43
Hi,

Say in a dialog i have a menu and i have added some actions to the menu


Qmenu *menu = new QMenu();
QAction *addGroupAction = new QAction("ADD", this);
QAction *renameGroupAction = new QAction("RENAME", this);
menu->addAction(addGroupAction);
menu->addAction(renameGroupAction);


No the question is
Whether the actions get deleted automatically when the menu is deleted.
and whether the menu is destroyed automatically when dialog is destoyed..


I mean have QT provided any such automatic garbage collection. or i have to do it explicitly by calling delete function.

Please help...

GK

aamer4yu
20th October 2008, 12:06
May be this (http://doc.trolltech.com/4.4/objecttrees.html) will answer your question :)

kaushal_gaurav
20th October 2008, 12:45
That is case of Parent-Child relationships...
but i am creation the QMenu instance on heap in the constructor of the dialog.
so does this mean when i close the dialog the menu object is destroyed...
and consequencty QAction objects are destroyed.
????

caduel
20th October 2008, 13:00
We don't know what you are doing with the menu. So we can not say what will happen.
i) If you create any object with new but do not set (or pass) a parent, it will usually not get deleted.
ii) It might get deleted, because you might have connected a signal to the QObject::deleteLater() slot, or a QWidget might have the Qt::WA_DeleteOnClose flag set.
iii) Why don't you create your popup menu on the stack?

QMenu menu;
menu.addAction(...);
...
QAction * sel = menu.exec();
This is faster and avoid any memory management issues.

HTH

wysota
20th October 2008, 17:26
That is case of Parent-Child relationships...
but i am creation the QMenu instance on heap in the constructor of the dialog.
so does this mean when i close the dialog the menu object is destroyed...
and consequencty QAction objects are destroyed.
????

You have basically three choices:
1) Take a look at Qt sources to see how addAction is implemented
2) Connect a slot to signal destroyed() of each action and see whether it fires when you destroy (destroy, not close) the menu
3) Think what would happen in this situation and which solution is more logical:

QAction *action = new QAction(...);
toolBar->addAction(action);
menu->addAction(action);
delete toolBar;