O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Deactivating Objects

We've learned that removing all references to an object makes that object eligible for garbage collection. However, even after an object becomes eligible for garbage collection, it continues to exist in memory until ActionScript decides to "sweep" it away during a garbage collection cycle. After the object becomes eligible for garbage collection, but before it is actually removed from system memory, the object continues to receive events and, in the case of Function objects, can still be triggered by setInterval( ).

For example, imagine a slideshow application that uses a class, ImageLoader, to load images from a server at regular intervals. The code for the ImageLoader class is as follows:

package {
  import flash.events.*;
  import flash.utils.*;

  public class ImageLoader {
    private var loadInterval:int;

    public function ImageLoader (delay:int = 1000) {
      loadInterval = setInterval(loadImage, delay);
    }

    public function loadImage ():void {
      trace("Now loading image...");
      // Image-loading code not shown
    }
  }
}

Further imagine that the application's main class, SlideShow, implements code to start and stop the slideshow. To start the slideshow, SlideShow creates an ImageLoader instance that manages image loading. The ImageLoader instance is stored in the instance variable imgLoader, as follows:

imgLoader = new ImageLoader();

To stop or pause a slideshow, SlideShow discards the ImageLoader instance, as follows:

imgLoader = null;

When imgLoader is set to null, the ImageLoader instance ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required