creepez
creepez

Reputation: 13

AS3 memory management

I have a question about as3 memory management.

For example if i have class

public class CustomizationScreen extends MovieClip {
       private var a:Display;
       public var b:Buttons;
       public function CustomizationScreen() {
           a = new Display(200,-20);
           b = new Buttons(900,-100,"Next");
           addChild(a);
           addChild(b);
       }
}

And i instantiate that class in main class

public class Main extends MovieClip {
   public var c:CustomizationScreen;
   public function Main() {
       c = new CustomizationScreen(200,-20);
       c.b.addEventListener(Event,func);
       addChild(c);
   }
   // func
}

Will this be enough for c object to be garbage collected?

c.removeEventListener(Event,func);
removeChild(c);
c=null;

Or do i also need to remove a and b object from c?

Upvotes: 1

Views: 101

Answers (2)

Pan
Pan

Reputation: 2101

You should define c in the class instead in the function

  public class Main extends MovieClip {

       private var c:CustomizationScreen;

       public function Main() {
            c = new CustomizationScreen(200,-20);
            c.b.addEventListener(Event,func);
            addChild(c);
       }

  }

and you need to remove eventListener on c.b, not on c.

public function dispose():void
{
     c.b.removeEventListener(Event,func);
     removeChild(c);
     c=null;
}

You'd better set a and b to null in CustomizationScreen, if there are reference to a and b from other class.

Upvotes: 1

payam_sbr
payam_sbr

Reputation: 1426

As you should know, garbage collection is not an ontime process and dont be done untill a valuable mount of memory is allocated..

The red line is our maximum allowed memory, then garbage called

but the most important thing about garbage ability of your object, is that there must not left any handler in your object which comunicates with other objects in out of your class. For example event listeners must be removed correctly.

Also take a look on this article https://code.tutsplus.com/tutorials/understanding-garbage-collection-in-as3--active-4412

And in answering your main question :
Yes. Your class will be successfully garbage collected!!

Upvotes: 1

Related Questions