I use a strong listener only when I know I'm not holding a reference to the 
dispatcher anywhere else. In all other cases, i.e. if I know I'm going to be 
keeping the dispatcher on the display list (if it's a DisplayObject) until I no 
longer need it, or if I know I'm storing a reference the dispatcher somewhere 
in a parent class or a dictionary or something, I always use a weak reference. 
That way, if I forget to remove the event listener in a destroy method or 
whatever, it should still end up eligible for GC.

Cases where the event doesn't fire because a weak reference was used only 
happen if there are no strong references anywhere else, so the object rightly 
gets GC'd

Piers


On 7 Feb 2010, at 13:27, Cor wrote:

> Could you give an example of the way you use the strong listener?
> 
> TIA
> 
> -----Original Message-----
> From: flashcoders-boun...@chattyfig.figleaf.com
> [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Steven Sacks
> Sent: zondag 7 februari 2010 4:32
> To: Flash Coders List
> Subject: Re: [Flashcoders] Re: is it ever ideal to NOT use weak references?
> 
> function loadImage():void
> {
>     var loader:URLLoader = new URLLoader();
>     loader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
>     loader.load(new URLRequest(url));
> }
> function onComplete(event:Event):void
> {
>     trace(event);
> }
> 
> Guess what never fires?  If you guessed "onComplete", you win the prize.
> 
> Now imagine that it defaulted to true and you wrote the same thing leaving
> off 
> the false, 0, true.  You would have no idea why it didn't work.
> 
> It didn't work because the loader was cleaned up by GC as soon as the 
> loadImage() function reached the end.
> 
> Generally, I only use weak listeners with Timers and enter frame listeners. 
> Everything else I use strong listeners and write a remove listener for them
> when 
> they're expected to unload or be garbage collected at some point.
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 9.0.733 / Virus Database: 271.1.1/2673 - Release Date: 02/07/10
> 08:22:00
> 
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to