Consider the following:
for (it = list.begin(); it != list.end(); ++it) {
if (!(*it)->alive) {
it = list.erase(it);
}
}
this works fine as long as list.size() > 1
. Once the list contains only one element, the call to erase segfaults. I am assuming because there is no next element. At least, that's the behavior I am observing.
Is there a correct way to handle this? Perhaps an entirely different approach?
As you can see I don't want to clear the list instantly. I want to just remove elements as they die and this works fine until it deletes the last element.
The following will work correctly:
for (it = list.begin(); it != list.end(); ) {
if (!(*it)->alive) {
it = list.erase(it);
} else {
++it;
}
}
The problem is not just with certain sizes. In your example, you are skipping an element every time you remove one. Think about it.
If the element is not "alive", then you do: it = list.erase(it);
, which removes the element and sets it
to the one that follows it. then your loop performs ++it
, which skips the next one, possibly skipping end()
!
This is not just an issue with skipping end()
, you were skipping others too, but removing the last element in the list will likely yield a crash.