Sign in to follow this  
Mussi

List iterator not incrementable

Recommended Posts

Mussi    4407
Hi, I have a little problem in my game which randomly pops up, so it seems. I get a Debug Assertion Failed error claiming that my list iterator is not incrementable. The call stack brings me to a simple piece of code:
std::list<Buff*>::iterator i = lstBuffs.begin();

	while(i != lstBuffs.end())
	{
		if((*i)->GetTimeLeft() == 0 && (*i)->GetDuration() > 0)
		{
			delete (*i);
			i = lstBuffs.erase(i);
		}
		else
		{
			(*i)->Update(pOverheadText, pPlayer, pEnemy);
			i++;
		}
	}
I don't see how the iterator could not increment. If the iterator should reach the end of the list then the loop should break. If the last element gets erased the iterator should get the same value as lstBuffs.end() and the loop should break. So I'm guessing that the iterator getting incremented is somehow corrupt. So the question is, why is this happening?

Share this post


Link to post
Share on other sites
Zipster    2359
Is it possible for any of those function calls to modify the list you're iterating? For instance, if the Update call removes the object from the list it would invalidate the iterator.

Share this post


Link to post
Share on other sites
Mussi    4407
Thanks for the reply. Good point, but too bad that's not the case.

I just noticed however that I don't get any error if I don't call list::clear(). This happens outside the function I posted before. I don't understand why it makes a difference, after a call to clear the list should be empty and the loop should not start at all, but this seems not to be the case.

Share this post


Link to post
Share on other sites
Mussi    4407
So I did some more debugging, I am certain this problem only occurs if I call lstBuffs.clear(). The weird thing is that at the time this error occurs the debugger shows me the list is empty. However if I check for an empty list using lstBuffs.empty() before I run the function I still get the same error.

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