PDA

View Full Version : What happens when i call clear() of QList<QObject *> variable?



ricardodovalle
14th March 2014, 21:16
I have the following code:




QList<QObject *> m_devices;
m_devices.append(new Data::Device(info));



When do I call the method m_devices.clear() from QList, the allocated memory (new Data::Device(info)) will be free correctly or I need do delete one by one before?

stampede
14th March 2014, 22:40
If "info" is a QObject and is assigned as parent to Data::Device objects, then memory will be freed after "info" object is deleted.
Otherwise you have to delete items manually, for example:

qDeleteAll(m_devices);
clear() will only remove the pointers from memory, but will not call "delete" on them.