Weak listeners are bad
The Flash Player has a problem with cleaning up memory for AS3. Storing a reference to an object blocks the Garbage Collector from disposing the object and the object will be kept in memory. That’s why a lot of developers set ‘useWeakReference’ to true when adding an event listener.
1 | addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void |
“A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.”
I think using weak listeners is bad practice - don’t do it!
When setting a listener as weak the responsibility of the operation of your application is transfered to the Garbage Collector, which isn’t reliable at all. If and when the Garbage Collector is running is completely random[1]. You never know if or when your listeners are removed, which makes your application unreliable and unstable. Weak listeners are used by lazy developers who don’t want to take responsibility for cleaning up their own code.
Removing event listeners should always be the responsibility of the developer (or application) and not the Garbage Collector. So if you add an event listener be sure you also remove it when the object should be disposed.
There is only one exception for using weak listeners: adding listeners to the stage. Since the stage will never be disposed and the only reference to an object is the reference to the stage, there is no need for this object to exist. But still you have to remove this (weak) listener when this object should be disposed!
Temple
If you do not want to remove all listers by hand, you could use Temple Library. This toolkit has a destruction model which automatically removes all (strong) event listeners when an object is destructed and makes the object available for garbage collection. But even if the Garbage Collector isn’t running your destructed objects won’t unnecessary listen for events.
The Temple is open-source and available on GoogleCode: http://code.google.com/p/templelibrary/
[1] I know there are some tricks to force a garbage collection, but those are all unsupported. So a Flash Player update could break those tricks.
October 8th, 2010 at 12:39
There is only one exception for using weak listeners: adding listeners to the stage. Since the stage will never be disposed and the only reference to an object is the reference to the stage, there is no need for this object to exist. But still you have to remove this (weak) listener when this object should be disposed!
I’m not sur with that, if you do it :
function MyMovieClip ()
{
addEventListener( Event.ADDED_TO_STAGE, _addedToStage );
}
when the object MyMovieClip is set to null, the listener will be cleaned,
October 8th, 2010 at 19:11
@Thomas: that’s because you set the listener on the object itself. So there is no reference to an other object, so the Garbage Collector can remove the object.
November 24th, 2010 at 14:36
Slightly OT, but I just wanted to say great work with Temple Library! I’m awed at the effort and thoroughness of the code, and simply releasing it as an Open Source effort is amazingly philanthropic.
I’m looking forward to some more documentation, though. Currently struggling with a site that uses a whole load of forms, and while the front-end interface is nicely and intuitively handled by the Form examples that you’ve posted, the actual processing, sending and receiving/parsing is harder to follow.
Not an enormous problem, as I’ll brew my own. But just a pity that I can’t use your implementation, as it would definitely be better!
November 24th, 2010 at 18:29
@Mani: thanks for your comment. We have plans to make a more detailed and extended example of the Form en the FormServices. But we are very busy at the moment, so I can’t give you any dates for this.
But I like that you are using the Temple. And if you have any problems or questions, please let us know, so we can improve our Temple Library. You can use the issue tracker on Google code ( http://code.google.com/p/templelibrary/issues/list ), for bugs and feature requests or Google groups ( http://groups.google.com/group/templelibrary ) for questions.
November 28th, 2010 at 21:40
@Mani: just added an example of the FormXMLService. I hope this makes the use of a FormService more clear:
http://templelibrary.googlecode.com/svn/trunk/examples/index.html#FormXMLService
March 22nd, 2011 at 11:34
I cannot open the example .fla’s for Temple Library in CS4 - are they all in CS5?
March 22nd, 2011 at 12:53
@Ted: We currently using CS5, that why all .fla files are CS5. But I just made a compatibility update to CS3. So you can open and run all examples in CS3, CS4 and CS5.
Version 2.8.6 is now available at GoogleCode: http://code.google.com/p/templelibrary/
March 24th, 2011 at 1:17
Thanks man that is great.
July 18th, 2011 at 17:22
I totally do not agree on this post.
Your argument was that if you use weak references you depend on the GC….
But using Temple framework for cleaning listeners….. thats just repositioning the scope of cleaning…. meaning now your own framework does it and your are dependant on that…
So thats actually same same..
Also.
Using weak references is actually a good thing, because:
If you dont clean it… aka forget to clean/remove it…… the GC will do it for you.
This is a solid approuch.. where as not using weak references and depending solely on self removing … is less solid thus bad practice.
so. I guess using them IS better.
Also the new FP 11 will have CG hinting which will make it even more reliable to use weakreferences.
July 19th, 2011 at 12:06
@Rackdoll: Thanks for your reply. But the difference between the GC and the Temple is that you can’t control the GC, but you can control the Temple! The Temple doesn’t destruct your objects automatically, it just destruct the objects if you first call the destruct() method. The Temple helps you destructing the object by removing all the event listeners. So this is absolutely not the same as how the GC works.
And if you don’t clean your event listeners, you probably don’t want to clean them, unless you are just a lazy programmer.
And this CG hinting you mention is only available for FP11, which is still in beta. So not many of the visitors will have this. So relaying on this would be silly. Till then you just can’t relay on the GC. You can never tell if or when the GC is running. So you can never tell for sure if an listener will react on an Event.