# List iterator not incrementable

## 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++;
}
}

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 on other sites
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 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 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978704

• 21
• 14
• 12
• 10
• 12