If we have a QMap<int, Something*>
does deleting the QMap properly clean up the Something*?
"Something" is not part of the object tree.
Printable View
If we have a QMap<int, Something*>
does deleting the QMap properly clean up the Something*?
"Something" is not part of the object tree.
Yes, destroying the QMap "properly" cleans up the Someting pointers it owns. That has nothing to with the Something instances those pointers point at, which continue to exist because the container does not own them.
What is the appropriate loop to deal with the instances?
example:
Code:
foreach (Something* thing, a_map) { delete thing; }
More like this:
Code:
QList< Something * > things = a_map.values(); foreach( Something * thing, things ) { delete thing; }
Edit: although upon reading the docs, it looks like your code would accomplish the same thing. I am more familiar with std:: map<>, where the iterator returns an std:: pair<> containing the key and value.
Definitely iterating over the map.
Iterating over the values requires two iterations: one for creating the list of values and the the actual loop.
In any case: http://doc.qt.io/qt-5/qtalgorithms.html#qDeleteAll-1
Cheers,
_