cykod / Quintus

HTML5 Game Engine
http://html5quintus.com
GNU General Public License v2.0
1.41k stars 401 forks source link

Calling this.destroy() in an event listener can cause an exception while processing other events of the same type #133

Closed vincentjames501 closed 10 years ago

vincentjames501 commented 10 years ago

The code below shows the problem

      if(this.listeners && this.listeners[event]) {
        // Call each listener in the context of either the target passed into
        // `on` or the object itself.
        for(var i=0,len = this.listeners[event].length;i<len;i++) {
          var listener = this.listeners[event][i];
          listener[1].call(listener[0],data); // If this listener calls destroy on itself, the list we're looping through can change and listener can be undefined
        }
      }

It would be better to build an Array of the functions you want to call, then execute them all so the list is immutable during iteration.

cykod commented 10 years ago

It would be better - but building one-use arrays on every step is terrible for GC, which is the primary performance issue Quintus has, so it's a no go.

If you're running into a problem - call stage.remove(sprite) which will put off the call to destroy until after the step