Simplifying Garbage Collection with CASA Lib

Written by Mike Creighton on December 21st, 2008

One of the most important things in ActionScript 3 is adhering to good garbage collection practices. With the release of CASA Lib for AS3, we’ve done our best to make that chore as easy as possible—extending the benefits to your own classes and work flow.

Every class that can be instantiated in CASA Lib for AS3 implements the IDetroyable interface. This means when it’s time to offer a given instance up for garbage collection, you only need to type this:

myCasaClass.destroy();
myCasaClass = null;

But we’ve gone an extra step to make garbage collection easier. With ActionScript 3 came a robust and reliable event model that simply wasn’t present in ActionScript 2. However, one of the chores that came along with this was having to “manually” remove all your event listeners when it was time to get rid of a class instance.

Every class in CASA Lib that’s capable of dispatching events either extends the RemoveableEventDispatcher class or implements the IRemovableEventDipatcher interface. When you call the destroy() method for these classes, all your event listeners are automatically removed. In addition to this, you’ve got access to a few very useful methods:

  • removeEventListeners - Removes all event listeners
  • removeEventsForListener - Removes all events associated with a specified listener
  • removeEventsForType - Removes all event listeners that were listening for a specific event type

All the display object classes in the org.casalib.display package not only implement the IRemovableEventDispatcher interface, but they also automatically remove themselves from their parent (if they’ve got one) when you call destroy().

To make the most of CASA Lib for AS3, when you would normally extend the core AS3 classes like EventDispatcher, MovieClip, Sprite, Bitmap, or TextField, you should start extending these CASA Lib classes:

Then, just override the destroy() method, adding the extra garbage cleanup that you need in your own classes. Finally, call super.destroy() in the last line of the overridden function.

Comments

Written by Sotiris Boukouvalas on December 21st, 2010

Hi there and thanks for sharing this library. Great work. One of my concerns the last days was the GC. So I was experimenting with your library. What I do is that when I load the external swf (which extends CasaMovieClip) I add one timer event that traces to me that it is still alive. When removing it what I do is that I remove all its childs and destroying it using this.removeChildrenAndDestroy();
As I see all children are removed successfully but not the events as the timer event is still running.

Are you suspecting something could go wrong with this scenario?

Thanks for the great work!

Written by Aaron Clinger on December 21st, 2010

Hi Sotiris,

CasaMovieClip will only automatically remove MovieClip/DisplayObject listeners or listeners added directly to the object. It will not automatically remove all listeners. If you want to also remove the event listeners for Timer, you can extend the destroy() method and remove the Timer events there. Additionally, instead of using Timer, you can use the CASA class Interval which has it’s own built in destroy method that you can call. Please let me know if you have anymore questions.

Here is an example:

override public function destroy():void {
  myTimer.stop();
  myTimer.removeEventListener(TimerEvent.TIMER, myCallBack);
 
  super.destroy();
}

Written by Sotiris Boukouvalas on December 21st, 2010

Of Course. How silly of me… Timer’s Class is still generic so its own addEventListener function is generic and does not add this event on our list of events. Thanks for your help.

By the way I see it works good !! Well done.

Written by dehash on December 30th, 2010

Useful library. Just a note that for ImageLoad I found destroy() did part of the job but imageLoad.contentAsBitmap.bitmapData.dispose() did the rest.

Written by Aaron Clinger on January 3rd, 2011

Thanks Dehash - you’re correct. When you destroy an ImageLoad you are only destroying the load, not the loaded image. This is because there are many times where one might only want to destroy the load, but keep the image data for use later in the application.

Written by Alexa on March 22nd, 2012

Hey, really like the blog, wordpress?

Comments are no longer accepted for this blog post.