1 Reply Latest reply on Mar 19, 2010 10:18 PM by Nyeski

    Even if I set a componet to null and remove from parent.. still importan to remove event listeners?

    rickcr Level 1

      I'm confused on something. My Essential Action Script book mentions that you really should remove all register event listeners when you no longer are using the component where there are defined. Does this even apply if you are removing the container from its parent and you set the object to null? Shouldn't the garbage collector take care of wiping out the registered event listener as well since it was registered in the component that you set to null?

        • 1. Re: Even if I set a componet to null and remove from parent.. still importan to remove event listeners?
          Nyeski Level 1

          When you set a component to null, what you are doing is setting that particular reference to null, not making the object itself null. The event listener retains a reference to the component, and since the reference count for that object is greater than zero, it cannot be garbage collected.

           

          You can get around this problem by setting the useWeakReference flag in the addEventListener() call to true. Be careful with this, though. If you use a weak reference and don't remove the event listener, you might still react to events that are thrown between the time when it's removed and when it's garbage collected. It's usually safe, but if there are any events that are triggered by a timer, it could be a problem.