views:

34

answers:

3

Is there a fast and efficient way of removing all children listeners etc from my app. If everything is contained in a display object on the stage called View? I have lots of dynamically called children and their listeners do not get removed when I remove the View they reside in.

    public function _discard ():void
    {
        // Quick way to discard the view, remove children and listeners
        removeChild(View);
        View = null;
    }

Is this a valid way of removing the parent and children?

+1  A: 

AFAIK, there is no way to enumerate listeners in ActionScript. So you have to write cleanup code, removeEventListener calls symmetrical to addEventListener.

alxx
A: 

alxx is right. there is no easy trick to solve this problem. In order to avoid removing manually all children/listeners, I would create a helper method that receives a View and recursively removes all children and their respective listeners.

jdecuyper
+1  A: 

If you use weakly referenced listeners then the listeners will not prevent the objects from being garbage collected when you nullify them.

useWeakReference is the 5th parameter in the addEventListener call.

Cadin
stange that they would not be weak by default.
woodscreative
Yeah. There's a bit of discussion about that here: http://stackoverflow.com/questions/1891869/why-does-as3-not-use-weak-references-by-default-in-event-listeners
Cadin
The docs on addEventListener are talking about listeners as functions, not objects containing them. It seems that this parameter only relevant for anonymous functions that are subject to garbage collection (correct me if I'm wrong).
alxx
I don't think that's true. You still need to clean up your objects when you're done with them. If you have an event listener attached to an object it counts as a reference to that object, so it will remain in memory even if you remove all your other references to it. A weak reference doesn't count as a reference to the object, so if you remove all other references to the object (but not the weak listener) the object will still get marked for garbage collection.
Cadin