Sign in to follow this  
y2jsave

is it invalid..

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this