I'm getting that error with this code:
for(std::vector<AguiTimedEvent*>::iterator it = timedEvents.begin();
it != timedEvents.end();)
{
if((*it)->expired())
{
(*it)->timedEventCallback();
delete (*it);
it = timedEvents.erase(it);
}
else
it++;
}
What could be the problem?
the timed event sometimes pushes a new one in when its callback is called, that might do it
Thanks
If you are looping through a vector and the callback function causes the vector to be added to, then all iterators into the vector may be invalidated including the loop variable it
.
In this case (where the callback modifies the vector) you are probably better off using an index as your loop variable.
You probably need to do some thorough analysis of the design to make sure that you aren't going to create any unexpected infinite loops.
E.g.
for(std::vector<AguiTimedEvent*>::size_type n = 0;
n < timedEvents.size();)
{
if(timedEvents[n]->expired())
{
timedEvents[n]->timedEventCallback();
delete timedEvents[n];
timedEvents.erase(timedEvents.begin() + n);
}
else
n++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With