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

15 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
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
  • Bought between 1 and 9 items
  • Brazil
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Sold between 100 and 1 000 dollars
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.

8194 posts Community Moderator
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 6-7 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX Volunteer 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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
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
  • Sold between 1 000 and 5 000 dollars
  • Has been a member for 5-6 years
  • Exclusive Author
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
  • United States
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
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United States
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 .
5311 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 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.

8194 posts Community Moderator
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 6-7 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX Volunteer 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
  • Bought between 1 and 9 items
  • Has been a member for 6-7 years
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
  • Bought between 1 and 9 items
  • Has been a member for 6-7 years
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