A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Should you keep removing addEventListener???

  1. #1
    Senior Member
    Join Date
    Jul 2004
    Posts
    264

    Should you keep removing addEventListener???

    Hi guys,


    When i add this:

    Actionscript Code:
    bob.addEventListener(MouseEvent.ROLL_OVER,funCtion);

    And many others --

    Should i keep removing them when they are not going to be used -- then re-add them when they are needed again.

    Or can i just leave them - and they will not take up any extra resources?


    In as2 -- if had on(release) on a button -- i woudlnt have to keep removing it - so i'm just seeing if i have to do it in AS3 with listeners?

    Thanks for the help!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm assuming you want to disable the handler. If you don't, then there's no reason to remove it. If you do want to disable it, you can either remove the handler as an event listener, or you can set a variable which you check for in the handler and immediately exit. Removing the handler will mean that the function is not even executed, so that's almost always more efficient. But if your logic for when to enable/disable the handler is complex, a state variable might be simpler to understand.

  3. #3
    Senior Member
    Join Date
    May 2010
    Posts
    178
    Simple is this, that if your listener is for a display object which is in the displayList, its no need to remove it. If it is for a Events and no need to keep it after executing the event, remove it. In case of adding ADDED_TO_STAGE Listener ..... remove on the execution function.

    So, all it depends on how you execute your application and what need to keep it run.



    poltuda

  4. #4
    Lifetime Friend of Site Staff Northcode's Avatar
    Join Date
    Dec 2000
    Location
    Whitehorse YT
    Posts
    3,764
    Not removing them may seem harmless but if you've ever wondered why your app is "leaking" memory you can stop blaming Flash... The listener ceates a reference to the object it's added to. If you want the garbage collector to clean up "unused" objects they can't have ANY references hanging around.
    When your swf2exe tool just HAS to work
    there's only one choice... SWF Studio

  5. #5
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    The easy rule is if you are finished using it then remove it otherwise keep it.
    ~calmchess~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center