object references in event handlers prevent GC

a bug in flash player 9 i came across today while trying to get a swf to unload completely:


if you have a reference to Keyboard or Capabilities (or, probably, a number of other native Classes as well) in an event handler, it will loiter as a pair of Class and Object references when the enclosing swf is unloaded via Loader.unload().

for example:
private function onKeyDown (evt:KeyboardEvent) :void {
    if (evt.keyCode == Keyboard.SPACE) { doStuff(); }

must be replaced with:
private function onKeyDown (evt:KeyboardEvent) :void {
    if (evt.keyCode == 32) { doStuff(); }

what a pain.


  1. makc says:

    what about Keyboard.SPACE inside doStuff ()?

  2. ericsoco says:

    haven’t tested that, but i imagine it’s fine. i think the problem with using it inside the event handler has to do with the way event handlers are stored in EventDispatcher instance queues; for some reason, using something like Keyboard in there keeps the event handler from getting properly removed from the EventDispatcher queue. calls from the event handler are not stored inside the EventDispatcher queue, and should not be an issue.

    just a guess.

Leave a Reply

Additional comments powered by BackType