Is it true to say that when passing an anonymous function to the
addEventListener function, you should never use weak references???

If you do, then wouldnt the listener (anonymous function) get garbage
collected as soon as the calling method returns????


Nikolay Iliev wrote:
> 
> Thanks for this information and the useful link.
> 
>  
> 
> As far as understand there is no need to use weak references when I want
> to
> add listener to a child of the current instance or to the current instance
> itself. 
> 
>  
> 
> Here is a more complete scenario:
> 
>  
> 
> EditSomething.mxml
> 
>  
> 
> <?xml version="1.0" encoding="utf-8"?>
> 
> <EditSomethingLogic xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml";
> width="400" height="300">
> 
>       <mx:Button id="btnSave" label="Save" />
> 
> </EditSomethingLogic>
> 
>  
> 
> EditSomethingLogic.as
> 
>  
> 
>  
> 
> package
> 
> {
> 
>       import flash.events.MouseEvent;
> 
>       
> 
>       import mx.containers.Canvas;
> 
>       import mx.controls.Button;
> 
>       import mx.events.FlexEvent;
> 
>  
> 
>       public class EditSomethingLogic extends Canvas
> 
>       {
> 
>             [Bindable]
> 
>             public var btnSave:Button;
> 
>             
> 
>             public function EditSomethingLogic() {
> 
>                   super();
> 
>                   
> 
>                   addEventListener(FlexEvent.CREATION_COMPLETE, onInit);
> 
> 
>             }
> 
>             
> 
>             private function onInit(event:FlexEvent):void {
> 
>                   btnSave.addEventListener(MouseEvent.CLICK,
> onBtnSaveClick);
> 
>             }
> 
>             
> 
>             private function onBtnSaveClick(event:MouseEvent):void {
> 
>                   // Do something - save some data
> 
>             }
> 
>       }
> 
> }
> 
>  
> 
>  
> 
> So in both cases (addEventListener(FlexEvent.CREATION_COMPLETE, onInit);
> and
> btnSave.addEventListener(MouseEvent.CLICK, onBtnSaveClick);) there is no
> point in using weak references as the whole EditSomething instance
> including
> the child sprites and the event listeners will be garbage collected once
> removed from the stage?
> 
>  
> 
> Kind Regards,
> 
> Nikolay
> 
>  
> 
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
> Behalf Of florian.salihovic
> Sent: Wednesday, November 05, 2008 3:50 PM
> To: flexcoders@yahoogroups.com
> Subject: [flexcoders] Re: Listeners and weak references
> 
>  
> 
> When u register an EventListener to en IEventDispatcher instance not using
> weak 
> references, the object won't be garbage collected untill all references
> got
> null. This not 
> only affects references by objects and arrays, but also eventlistener.
> Using
> weak references 
> will allow the garbage collection to remove objects, even if there are
> eventlisteners left.
> 
> But using weak references will also slow the machine down, if i remember
> right aboit 
> 10clock cycles.
> 
> Here's a blog entry u might want to read:
> http://www.onflex.org/ted/2008/09/useweakreferencesboolean-false.php
> 
> I was using weak refernces heavily, but u don't have to.
> 
> When u are using eventlistener as members of the instance, they don't have
> to be weak, 
> since the whole object can be garbage collected.
> 
> But, when u have for example a sprite instanciated in the scope of another
> object, u 
> should for example use the eventlistener for Event.ADDED weak referenced.
> The other 
> listeners will be added and removed depending on the state of sprite -> an
> example; why 
> should a sprite listen to mouse events, when it is not in the displaylist.
> Add the necessary 
> listeners when it is on the stage.
> 
> So it always depends on what u are doing.
> 
> Best regards.
> 
> --- In flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> ,
> "iliev_niki" <[EMAIL PROTECTED]> wrote:
>>
>> I have a question on the weak references when adding a listener like 
>> this:
>> 
>> btnSave.addEventListener(MouseEvent.CLICK, onBtnSaveClick, false, 0, 
>> true);
>> 
>> I have found this approach with weak reference as an advise 
>> somewhere, but I would like to understand what happens really.
>> 
>> If we add a listener specifying useWeakReference = true as in the 
>> example above, does this mean that the garbage collection might 
>> remove unexpectedly the event listener and the mouse event handling 
>> to stop working?
>> 
>> When using weak reference for the event listener, is it needed to 
>> explicitly remove the event listener at some point in time?
>> 
>> In general - what is the best practice in this regard?
>> 
>> Kind Regards,
>> Nikolay
>>
> 
>  
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Listeners-and-weak-references-tp20341645p20343429.html
Sent from the FlexCoders mailing list archive at Nabble.com.

Reply via email to