>  The common use for weak event
>  listeners is when adding listeners to the stage

I disagree. Weak event listeners should be used in nearly all cases.

>  In most cases there is actually no real benefit of creating weak event
>  listeners. In most cases parents add
>  listeners to their children. Children are removed and that's that.

This is not correct. If you add a normal event listener to a child,
and then remove that child, the child is _not_ garbage collected.
Objects are only garbage collected when all references to the object
are removed. Adding a normal event listener causes a reference to be
created invisibly within the event system.

You must remove all normal listeners from an object before it will be
garbage collected.

> Hmmm, if it was garbage collected, why would I remove the listener, isnt
>  that the point of weak references?

You can't use weak references on an anonymous event listener. The only
reference to that function is in the event system itself, so if you
use a weak reference, the garbage collector considers the anonymous
function eligible for garbage collection. Thus, the garbage collector
removes your event listener immediately, and you never get your event.

(When I say "immediately," I really mean "at a random time when the GC
is invoked." Sometimes that's immediate, sometimes its not, but you
should assume immediacy so your code doesn't randomly break.)

On 3/13/08, EECOLOR <[EMAIL PROTECTED]> wrote:
> >That's a dangerous practice.
>
>
> I do not completely agree.
>
>
>  >It prevents you from using weak event listeners
>
>
> Correct.
>
>  The common use for weak event
>  listeners is when adding listeners to the stage: the stage is allways
>  present and adding normal listeners to it will insure that the
>  listener will persist. Even if it is removed everywhere else.
>
>  In most cases there is actually no real benefit of creating weak event
>  listeners. In most cases parents add
>  listeners to their children. Children are removed and that's that. If
>  the parent gets removed: no problem.
>
>  In the majority of the cases, the creation of listeners on a parent is bad
>  practice (stage is one of those exceptions).
>
>
>
>  > Now have to manually remove your listener to
>  > make sure your object is garbage collected -- but the only way to
>  > remove event listeners is to pass the removeEventListener function a
>  > reference to the function you want to remove... but you don't have
>  > that reference!
>
>
> Hmmm, if it was garbage collected, why would I remove the listener, isnt
>  that the point of weak references?
>
>
>  Greetz Erik
>
>
>
>  On 3/12/08, Cory Petosky <[EMAIL PROTECTED]> wrote:
>  >
>
> > That's a dangerous practice. It prevents you from using weak event
>  > listeners -- because the only reference to your delegate function
>  > object is the event listener itself, a weakly-referenced listener will
>  > be garbage collected. Now have to manually remove your listener to
>  > make sure your object is garbage collected -- but the only way to
>  > remove event listeners is to pass the removeEventListener function a
>  > reference to the function you want to remove... but you don't have
>  > that reference!
>  >
>  > If it's a one-time event, you can have the listener remove itself.
>  > Your delegate function object can pass a reference to itself (using
>  > arguments.callee) in to the "real" event listener function, which you
>  > can use to remove the listener. In any other case, you'll have to
>  > build a separate data structure (usually a Dictionary of Objects ->
>  > Functions) to keep a reference available to your code.
>  >
>
> _______________________________________________
>  Flashcoders mailing list
>  Flashcoders@chattyfig.figleaf.com
>  http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>


-- 
Cory Petosky : Lead Developer : PUNY
1618 Central Ave NE Suite 130
Minneapolis, MN 55413
Office: 612.216.3924
Mobile: 240.422.9652
Fax: 612.605.9216
http://www.punyentertainment.com
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to