Donate to protect them directly or help international organizations.
Weak Event Listeners Warning
November 15th, 2008
An object that has no references to it will be eventually garbage-collected. An event listener is also a reference, thus preventing it from being removed from memory.
In order to allow your objects to be garbage-collected, you can either remove all listeners from it or use weak event listeners instead (don't forget to remove other references to it as well). To make a listener weak, you must set the 5th parameter of the addEventListener method to true, like this:
var btn:Button = new Button(); btn.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
But you have to be very careful where you use weak event listeners. When you instantiate an object in a function's scope and then add weak event listeners, your object could be garbage-collected before it had a chance to dispatch these events. Example:
private function getStuff():void { var service:HTTPService = new HTTPService(); service.url = "get-stuff.php"; service.addEventListener(ResultEvent.RESULT, resultHandler, false, 0, true); }
In this case, you should either avoid using weak listeners and remove them manually, or add a reference to your object outside of the function's scope to prevent it from being garbage-collected too early.
Previous: Garbage collection with SWF loading Next: if (value is Object) problem