flash interactivity_traps_garbage_collector

The traps of the garbage collector

As we previously saw the garbage collector is one of the essential elements to take into consideration in ActionScript 3 application development.

In the previous example we used the Tween class in order to manage the movement of each button. An old habit coming from previous versions of ActionScript could lead us to not keep the references to Tween objects.

In the following code, we have modified the createMenu function so that it doesn’t store references to the Tween object:

function createMenu ():void
{
var lng:int = captions.length;
var myButton:Button;
var tweenMovement:Tween;
var tweenRollover:Tween;
for ( var i:int = 0; i< lng; i++ )
{
// creation of instances of the Button symbol
myButton = new Button();
// variable referencing the clip used for the upState
var upState:MovieClip = MovieClip ( myButton.upState );
upState.myCaption.text = captions[i];
// modification of clip for all states
myButton.upState = upState;
myButton.downState = upState
myButton.overState = upState
myButton.hitTestState = upState
// arrangement of instances
tweenMovement = new Tween ( myButton, "y", Elastic.easeOut, 0, 20 +
i * (myButton.height + 10), 3, true );
// a Tween object is created for the Rollover effects
tweenRollover = new Tween ( upState.backgroundButton, "scaleX",
Elastic.easeOut, 1, 1, 2, true );
container.addChild ( myButton );
}
}

Once the createMenu function is executed the tweenMovement and tweenRollover variables are deleted. Our Tween objects are no longer referenced in our application. If we manually trigger the passing of the garbage collector after the creation of the menu:

createMenu();
// trigger of garbage collector
System.gc();

We can see that the movement of each button is interrupted as the garbage collector has just deleted the Tween objects from the memory.

Worth remembering

  • Make sure you reference each necessary object so that they don’t get deleted by the garbage collector by accident.







Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42