If we have a QMap<int, Something*>
does deleting the QMap properly clean up the Something*?
"Something" is not part of the object tree.
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.
ravas (13th November 2015)
What is the appropriate loop to deal with the instances?
example:
Qt Code:
foreach (Something* thing, a_map) { delete thing; }To copy to clipboard, switch view to plain text mode
More like this:
Qt Code:
QList< Something * > things = a_map.values(); foreach( Something * thing, things ) { delete thing; }To copy to clipboard, switch view to plain text mode
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.
Last edited by d_stranz; 13th November 2015 at 22:13.
ravas (14th November 2015)
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,
_
Bookmarks