• Advertisement
Sign in to follow this  

is it invalid..

This topic is 2861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI , Is it invalid ,, and lead to valgrind related issues .. if yes, please suggest a alternative .. map<string,A*>::iterator start = _maps->begin(); map<string,A*>::iterator end = _maps->end(); for( ; start != end ; start++) { string Name = (*start).first; if(Name == "me") { _maps->erase(Name); } } Regards..

Share this post


Link to post
Share on other sites
Advertisement

map<string, A*>::iterator it = _maps->find( "me" );
delete it->second; // Does _maps own the A* pointers? If so, you need to delete it before removing it from the map
_maps->erase( it );

Share this post


Link to post
Share on other sites
Yes that is invalid -- you're erasing the element pointed to by the iterator, which makes the iterator invalid.
After calling erase, the next call to "start++" (which should be "++start" by the way) will cause bad things to happen (because "start" is no longer a valid iterator).
Try:
map<string,A*>::iterator start = _maps->begin();
map<string,A*>::iterator end = _maps->end();
while( start != end )
{
map<string,A*>::iterator current = start;
++start;
if(current->first == "me")
{
_maps->erase(current);
}
}
[Edit]If you're trying to find/erase a specific item, then you should use the find function like _fastcall posted above instead of writing the loop yourself ;)

Share this post


Link to post
Share on other sites
thanks a lot guys ...
i will try these changes and again check valgrind results ..

Thanks Again ..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement