Skip to main content
Inspiring
February 10, 2013
Question

Quick GC question

  • February 10, 2013
  • 1 reply
  • 615 views

Hello guys, I've a very simple question regarding GC.

I've created a Class A, which creates 8-10 sprites and adds them.

Now from my Main Class, I create an instance of Class A and listen for its completion.

When Class A dispatches Event.COMPLETE event, what I simple do from my Main Class is:

removeChild(Class A instance);

removeEventListener(...);

Class A instance name = null;

What I really don't do is, I don't delete those 8-10 sprites from Class A, as Class A itself will be removed.

So my question is, in this scenario, is it required to remove those 8-10 sprites of Class A, when Class A itself is removed and all reference to Class A is deleted/nullfied?

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
February 10, 2013

You are best off removing the Sprites.  Depending on how they are added, they may not be removed at all.

Inspiring
February 10, 2013

@Ned Murphy,

thanks for the reply. My script is something like this:

This is the Main class:

package

{

  public class Main extends Sprite

  {

    private var rectangles:Rects;

    public function Main():void

    {

      var te:Array = ["10 X 10", "20 X 20", "30 X 30"];

      rectangles = new Rects(te);

      rectangles.addEventListener(Event.SELECT, rectSelected);

    }

    private function rectSelected(e:Event):void

    {

       trace('a rect has been selected');

       rectangles.removeEventListener(Event.SELECT, rectSelected);

       rectangles = null;

    }

  }

}

Here is the Rects class

package

{

  public class Rects extends Sprite

  {

     public function Rects(ar:Array):void

     {

        var sprs:Array = new Array();

        for (var i:uint = 0; i < ar.length; i++)

        {

          sprs = new Sprite();

          sprs.graphics.beginFill(....);

          sprs.graphics.drawRect(....);

          sprs.graphics.endFill();

          sprs.addEventListener(MouseEvent.MOUSE_DOWN, sprSelected);

        }

     }

     private function sprSelected(e:MouseEvent):void

     {

         this.dispatchEvent(new Event(Event.SELECT));

     }

  }

}

Now notice that in Main class, when I receive an Event.SELECT, i no longer require Rects class instance anymore, so what I do is, I remove event-listener and nullfied its variable. Now, according to me, Rects class is not reference from any active object and hence should be swept by GC. I know, I could delete sprite invidually by creating a function in Rects class... etc, but I want to make sure if I do not do it, will the sprites of Rects be deleted by GC?