How come the Actionscript 3 ENTER_FRAME event is crazy nuts?

Posted by nstory on Stack Overflow See other posts from Stack Overflow or by nstory
Published on 2010-06-13T16:12:49Z Indexed on 2010/06/13 18:32 UTC
Read the original article Hit count: 189

Filed under:
|

So, I've been toying around with Flash, browsing through the documentation, and all that, and noticed that the ENTER_FRAME event seems to defy my expectation of a deterministic universe.
Take the following example:

(new MovieClip()).addEventListener(Event.ENTER_FRAME, 
     function(ev) {trace("Test");});

Notice this anonymous MovieClip is not added to the display hierarchy, and any reference to it is immediately lost.

It will actually print "Test" once a frame until it is garbage collected. How insane is that? The behavior of this is actually determined by when the garbage collector feels like coming around in all its unpredictable insanity! Is there a better way to create intermittent failures? Seriously.

My two theories are that either the DisplayObject class stores weak references to all its instances for the purpose of dispatching ENTER_FRAME events, or, and much wilder, the Flash player actually scans the heap each frame looking for ENTER_FRAME listeners to pull on.

Can any hardened Actionscript developer clue me in on how this works? (And maybe a why - the - f**k they thought this was a good idea?)

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about events