Skip to main content
January 29, 2010
Answered

add listener / remove listener

  • January 29, 2010
  • 1 reply
  • 833 views

hi,

when I add a listener on a clip or button (like the CLICK Event) that exists in the timeline do I allways have to remove the listener if the Clip is removed from stage? Or will it be removed automaticly by the garbage collection?

TIA

This topic has been closed for replies.
Correct answer

>button_mc.addEventListener(Event.REMOVED_FROM_STAGE,onRfS);

Yes, that is using strong reference and will not be GC'd. You should just always use weak references.

1 reply

January 29, 2010

It will only be removed automatically if you use a weak reference:

clip.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true); //weak ref

vs:

clip.addEventListener(MouseEvent.CLICK, clickHandler); //strong ref

the second version is only removed if you removeEventListener on it.

January 29, 2010

thx,

may be a stupid question:

same for the REMOVED_FROM_STAGE handler? I mean do I have to remove this too?

button_mc.addEventListener(Event.REMOVED_FROM_STAGE,onRfS);

Correct answer
January 29, 2010

>button_mc.addEventListener(Event.REMOVED_FROM_STAGE,onRfS);

Yes, that is using strong reference and will not be GC'd. You should just always use weak references.