I am trying to move some objects around in a std::list while looping through it. It works, but if isActive() returns false, the program crashes.
I am sure it has something to do with how I move the data around in the std::list, but I am not sure how to fix it. Could someone please give me a hand?
Code:
std::list<Object*> myList;
// Some code to add things to the list...
std::list<Object*>::iterator it = myList.begin();
std::list<Object*>::iterator tempIt;
// Update all the active objects in the list
while (it != myList.end())
{
// Check if the object is active
if((*it)->isActive())
{
// Update the object, if update failes, move it to the front
// of the list.
if( !( (*it)->update() ) )
{
// Assing the current object to a temp itterator
tempIt = it;
// Move the main itterator to the next object
it++;
// Insert the object that failed the update to the start of the list
myList.splice(tempIt, myList,myList.begin());
}
else
it++;
}
}