I would agree that's not a best practice for writing timer code, just the
quickest example I could think of. When people read about how bad strong
references are and how they cause memory leaks, they tend to make EVERYTHING
weak which can lead to some nasty problems. Since you can't control the
timing or depend on garbage collection, 95% of the time the listener could
get called, the other 5% the target object could be GCd before the listener
ever runs.

On Mon, Jul 14, 2008 at 11:49 PM, Josh McDonald <[EMAIL PROTECTED]> wrote:

>   Why use weak references on singletons or things that are going to live
> the life of the application? Not saying it's wrong, just interested in the
> logic.
>
> The timer code above is a great example of when you need a strong
> reference, but a nasty example of how to use a timer! I *really* don't like
> the idea of the Timer instance only being available within the event
> handlers :)
>
> A listener will always get called if the object it's listening to is still
> around though right? Weak reference or no?
>
> -Josh
>
> On Tue, Jul 15, 2008 at 2:43 PM, Tim Rowe <[EMAIL PROTECTED]>
> wrote:
>
>>  Use strong references when you're adding an event listener to the same
>> instance dispatching an event.  If it's an anonymous function, you need to
>> use a strong reference.  Also, use strong references when you want speed –
>> weak references are slower.
>>
>> Always use weak references when dealing with singletons (Cairngorm's event
>> dispatcher is a singleton, hence, always use with Cairngorm), or objects
>> that live the life of the application.  This includes timers waiting on call
>> backs also.
>>
>>
>>
>> Not always true but if you have an object you intend to never have
>> destroyed, but it may also be perfectly safe to use strong references.  Keep
>> in mind though that using a weak reference on, say, an event listener where
>> a strong one was required may result in things like that listener not
>> getting called.
>>
>>
>>
>> I'm only new to Flex, but this is as I understand it, and no doubt I've
>> missed a few things here and there.
>>
>>
>>
>> --Tim Rowe
>>   ------------------------------
>>
>> *From:* [email protected] [mailto:[EMAIL PROTECTED] *On
>> Behalf Of *Boon Chew
>> *Sent:* Tuesday, 15 July 2008 2:08 PM
>> *To:* [email protected]
>> *Subject:* [flexcoders] When not to use weak references?
>>
>>
>>
>> Hi all,
>>
>> I have read posts that preached the goodness of weak references for event
>> listening, but have not read anything that suggests when you should use
>> strong reference over the weak one, and  the down side to using weak
>> references.
>>
>> Any ideas when being weak is not bad thing? :)
>>
>> - boon
>>
>>
>>
>>
>
>
> --
> "Therefore, send not to know For whom the bell tolls. It tolls for thee."
>
> :: Josh 'G-Funk' McDonald
> :: 0437 221 380 :: [EMAIL PROTECTED]
>  
>

Reply via email to