std::list<cHBGameObject*>::iterator i;
//remove any object that has passed the cutoff
if(!lObjects.empty())
{
i = lObjects.begin();
while (i != lObjects.end())
{
if((*i)->checkIfOverDeletionThreshold(xDeletionPoint))
lObjects.erase(i);
else
++i;
}
}
In this case, the crash occurs if there is only 1 remaining object and the object is deleted, then when it returns to the "while" statement, the error is thrown.
I can sort of see where I'm going wrong, I think when it hits "while", because the list is empty, it may be that the iterator no longer points to anything valid. I have looked up examples to make sure I'm not doing it wrong but every analogous example I have seen have suggested this approach to looping through a list where you may delete objects. Besides, being able to delete objects in this manner is one of lists' advantages over vectors, no?
Could someone kindly point out where I am going wrong?