PDA

View Full Version : What to free or not to free, that is the question



bruccutler
25th July 2007, 00:12
When creating widgets in Qt, what do I need to free. Once I do the mainWidget.setLayout(layoutName), do I need to free any memory other than the widget itself? What about icon.setIcon(), does it take over the memory management for the icon?

I'll just include a sample file. Have I forgotten to free anything in the attached module, as long as I free the parent widget in the calling routine?

- BRC

marcel
25th July 2007, 05:04
Everything seems OK with your code.
Also take a look at: http://www.qtcentre.org/forum/f-qt-programming-2/t-memory-leak-in-my-application--5956.html

Generally you don't need to delete widgets, only in special cases when you allocate temporary widgets without a parent on the heap.

Also you might allocate QObject's on the heap, like a QList, QThread,ect. These are better to explicitly be deleted when finished using.

About setIcon: you can only pass a reference to this function, so if you have something like this:


QIcon *icon = new QIcon(...);
setIcon(*icon);

...
delete icon;

You must delete the icon, since the for which you set it owns a copy of your icon.

Regards
Regards