• Advertisement
Sign in to follow this  

List iterator not incrementable

This topic is 2883 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, 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
Advertisement
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
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
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
Turns out you were right Zipster, function inside a function inside another function called the clear function, totally missed that. Thanks again.

Share this post


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

  • Advertisement