AS3 - how to remove children/subchildren and all associated event listeners from memory

15 posts
  • Has been part of the Envato Community for over 6 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
marktaylor46 says

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.

Anyone with any ideas?

Thanks,

Mark.

563 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Located in Brazil
+2 more
LuizZak says

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:

object.addEventListener(eventType, function, bubbles, priority, useWeakReference);

Set it as true and the weak reference will not count towards as reference when doing garbage collecion.

8271 posts Community Moderator
  • Has referred 10+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Attended one of our Meetups around the world
+9 more
MSFX Moderator says

you can test if they have listeners using object.hasEventListener(type)

In FP10 you can use stopAndUnload() but I think this is only for externally loaded content.. not sure ….

you could weakly reference your event listeners on the objects too then its likely the garbage collection may take them out of memory…?

15 posts
  • Has been part of the Envato Community for over 6 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
marktaylor46 says

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.

Any other ideas?

Thanks, Mark.

70 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Has been part of the Envato Community for over 5 years
+2 more
ryguy says

Have you taken a look at Grant Skinner’s Janitor class ?

You can download it here: Janitor class

If you’re using a loader, you should remove listeners and any other references, then use the unload() method.

Unload method reference()

2309 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 10+ members
  • Has sold $100+ on Envato Market
  • Has been a beta tester for an Envato feature
+5 more
theflyingtinman says

It’s not always possible …

http://www.gskinner.com/blog/archives/2008/04/failure_to_unlo.html

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 .
5360 posts The Dude Abides
  • Located in United States
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+9 more
CodingJack says

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.

8271 posts Community Moderator
  • Has referred 10+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Attended one of our Meetups around the world
+9 more
MSFX Moderator says
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.

This is personally what I do with every piece of loaded content, I also have addListeners() and removeListener() functions.

If there is another way then i’m all ears :)

2 posts
  • Has been part of the Envato Community for over 6 years
  • Has collected 1+ items on Envato Market
oyster says

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

2 posts
  • Has been part of the Envato Community for over 6 years
  • Has collected 1+ items on Envato Market
oyster says

Ok here’s my fix for my issues commented above.

When the nested swf is deleted. Code on it’s timeline has a listener for a child clip on the timeline. In this case “bear_mc”

stage.addEventListener(Event.ENTER_FRAME, moveBg, false, 0, true ); stage.addEventListener(Event.ENTER_FRAME, moveTracker, false, 0, true); bear_mc.addEventListener(Event.REMOVED_FROM_STAGE, deleteG);

All the listeners with the original top two causing the errors

then
function deleteG(evt:Event):void { stage.removeEventListener(Event.ENTER_FRAME, moveBg); stage.removeEventListener(Event.ENTER_FRAME, moveTracker); }

This fixed my errors of NULL references being thrown in the parent.

Hope this helps

by
by
by
by
by
by