MattRix / Futile

A super simple Unity 2D framework
http://struct.ca/futile
833 stars 130 forks source link

Stoping a delayed callback inside a callback can make weird things #231

Open jpsarda opened 10 years ago

jpsarda commented 10 years ago

If you stop a delayed while callback are processed, it changes the array that is currently browsed and can make weird things or crashes.

My solution was to postpone the removing of the delayed call back like this

public void StopDelayedCall(Action func)
{
    int count = _delayedCallbacks.Count;

    for (int d = 0; d<count; d++)
    {
        FDelayedCallback call = _delayedCallbacks[d];

        if(call.func == func)
        {
            _delayedCallbacksToRemove.Add(call);

        }
    }
}

private void ProcessDelayedCallbacks()
{
    foreach (FDelayedCallback callToRemove in _delayedCallbacksToRemove) {
        _delayedCallbacks.Remove(callToRemove);
    }
    // ... same code after this
MattRix commented 10 years ago

Cool, makes sense, yup I'll put a fix in for it...