I have a complex as3 application that builds many composite moveiclip objects inside one another. I want a mechanism that will allow me to delete the highest level movie clip (the parent of all the others inside) that will also remove all the children from the display list and from memory and all their associated event listeners.
it’s easy to remove the children from the display list by just looping on numChildren and using removeChild. but that doesn’t remove the display objects from memory nor does it remove their event listeners.
Instead of manually removing the events, you can mark them as having a weak reference. Weak reference listeners does not count on the garbage collection process. Do this in right in the addEventListener function:
As far as I understand it (and i may be wrong) - useWeakReference set to true will still not garbage collect until the objectt the event is set on is set to null. Since some of my objects are 10-15 levels deep in other movieclips I would fid it difficult o do this. I wanted to be able to get rid of the owning parent movieclip remove all decendants from the diplay list, remove the object and remove their event listeners.
The only other way I can think of doing it is manually by writing a “killme” function for every one of my class/functions but this is rather onerous.
*Flash Player 9 Bug*
Flash Player 9 has a critical bug that prevents unloading loaded SWFs that have any active ENTER_FRAME or TIMER event listeners. This occurs regardless of whether these listeners are all within the scope of the loaded SWF, or even if they all use weak references. This also affect timeline code written in Flash authoring - *if you have any code in your timeline, it will not be possible to unload that SWF.*
Yes unfortunately a killMe type function is necessary. You can register everything with Skinner’s Janitor class and then add a REMOVED_FROM_STAGE listener and then call the Janitor inside it’s function. This way you can run your loop and when the object is removed from the stage the Janitor will take care of the rest.
This is an excellent discussion. I am having issues with implementing nested timeline references hanging about and throwing NULL errors in the parent when the swf is deleted.
I don’t have a solution. My development relies heavily upon this kind of architecture.
I’m not seeing solutions here as yet but will update when i have sported this out.
Cheers oyster