AS3 EventManager 1.23: cleanUp method added

A major reason why some users of my EventManager class may see more memory leaks being shown than actual memory leaks is because EventManager stores all the listeners in a dictionary. If we remove a display object then technically (if weakReference is set to true) the listeners are removed for the DisplayObject and its children. Until today, EventManager required coders to manually remove every listener they created with EventManager even if Adobe’s GC actually removed them.

As much as I like the listeners being auto removed, I feel it’s better in practice to manually remove them so you know what’s going on while you code. If you always depend on that to happen then you might find yourself with memory leaks.

I never thought to actually have EventManager check each object to see if it’ll trigger the Event EventManager says it has. Therefore, I added cleanUp which will go through all of the listeners and remove the ones that don’t trigger anymore. This new method means that the EventManager should be more accurate in what’s going on (especially if you call a deepTrace now). It won’t work all the time of course (multiple listeners in one object will still be reported as a leak unless garbage collection takes care of it or you use EventDispatcher’s removeEventListener to remove them).

Anyway, check it out.

0 thoughts on “AS3 EventManager 1.23: cleanUp method added”

  1. Soren,
    Just create an .as file called EventManager and paste that content into it. Depending on what folder you’re in you’ll need to change k2xl.util under the package line to whatever you’re using.

Leave a Reply

Your email address will not be published. Required fields are marked *