Hi. There is an item exactly on this issue (deleting iterators) in S.Meyers "Effective STL". I think there was something about incrementing too. Regards, Dima. On 5/15/06, Peter Doerfler wrote:
Aseem Rastogi wrote: > could be because when u have erased the map node pointed to by iterMap, > iterMap++ crashes as the memory location pointed to by iterMap becomes > invalid. > > so, instead of erasing in loop, do a clear () on map after the loop. > Alternatively, if you can't do that you can increment first and do the erase() then. This creates a bit of overhead and a while loop is more appropriate -- along these lines: while (iterMap != iterMapEnd) { CS_DATAFMT* obj = iterMap->second; if (obj != NULL) delete obj; this->colDtlsByName.erase(iterMap++); } In your example, where you actually erase all nodes, the clear() method is more appropriate, but if you need to remove nodes conditionally, the above is an option. I also seem to recall reading that std::map::erase will return an iterator pointing to the next element in the future which would simplify things.... Found it: It's on the v7 branch. Best, Peter