Unsolved deleting a QMap after clear()
-
Hi,
is it safe to call delete directly after having called clear() on a QMap.
e.g. :QMap *myMap = new QMap<QString,MyClass>
myMap->insert("one", myClass);
....
myMap->clear();
delete myMap; -
hi and welcome to the forums
yes it should be.
The clear happens in place and will delete the internal structures. -
If your value objects are pointer type, you should delete them as well.
will delete the internal structures.
@mrjj curious about above statement. What do you mean by it ? -
@dheerendra
I simply meant that calling clear deletes QMaps internal structures holding the items it manages.
But good call noting if MyClass is a pointer , clear() will not be delete the object being pointed to, but only the pointer itself. -
And to complete it all together - there is no need to create a QMap on the heap at all in most of the cases.