Skip to main content
October 4, 2013
Answered

nulling a movieclip made from a class - does it make it trashable?

  • October 4, 2013
  • 1 reply
  • 1238 views

Hi

I instantiate a class that makes some movieclips within it, and from the class that instatiated it, I add some event listeners to the moveiclips...

(simplified)

var myClip:MCClass = new MCClass();

myClip.mc1.addEventListener(MouseEvent.CLICK, doSomething);

If at some stage I state myClip = null, does that make the event listeners eligible to be garbage collected, or do I need to removeEventListener them prior to myClip = null?

Cheers!

This topic has been closed for replies.
Correct answer Amy Blankenship

You have to both remove MyClip from the stage and also make it null to make the object that has the functions that are being used as event listeners available for garbage collection.

For example:

public class Obj1 {

     protected var _obj2:Obj2;

     public function get obj2():Obj2 {

          return _obj2;

     }

     public function set obj2(value:Obj2):void {

          if (value != _obj2) {

               //should do this so the event listeners don't fire after obj2 nulled in larger context

               if (_obj2) {

                    _obj2.child.removeEventListener('someEvent', listener);

               }

               _obj2 = value;

               if (_obj2) {

                    _obj2.child.addEventListener('someEvent', listener);

               }

          }

     }

     protected function listener(e:Event):void {

          //do something

     }

}

public class MainClass {    

     protected var _obj1:Obj1;

     protected var _obj2:Obj2;

...

     protected function init():void {    

          _obj2 = new Obj2();

          addChild(_obj2);

          _obj1 = new Obj1();

          _obj1.obj2 = _obj2;

     }

     protected function tearDown():void {

          removeChild(_obj2);

          _obj2 = null;

          //if you don't null _obj1.obj2, the listener will keep firing until it is garbage collected

          //but obj1 will eventually get garbage collected

          _obj1=null;

     }

}

1 reply

Amy Blankenship
Amy BlankenshipCorrect answer
Brainiac
October 4, 2013

You have to both remove MyClip from the stage and also make it null to make the object that has the functions that are being used as event listeners available for garbage collection.

For example:

public class Obj1 {

     protected var _obj2:Obj2;

     public function get obj2():Obj2 {

          return _obj2;

     }

     public function set obj2(value:Obj2):void {

          if (value != _obj2) {

               //should do this so the event listeners don't fire after obj2 nulled in larger context

               if (_obj2) {

                    _obj2.child.removeEventListener('someEvent', listener);

               }

               _obj2 = value;

               if (_obj2) {

                    _obj2.child.addEventListener('someEvent', listener);

               }

          }

     }

     protected function listener(e:Event):void {

          //do something

     }

}

public class MainClass {    

     protected var _obj1:Obj1;

     protected var _obj2:Obj2;

...

     protected function init():void {    

          _obj2 = new Obj2();

          addChild(_obj2);

          _obj1 = new Obj1();

          _obj1.obj2 = _obj2;

     }

     protected function tearDown():void {

          removeChild(_obj2);

          _obj2 = null;

          //if you don't null _obj1.obj2, the listener will keep firing until it is garbage collected

          //but obj1 will eventually get garbage collected

          _obj1=null;

     }

}

October 4, 2013

Thank you Amy!

Amy Blankenship
Brainiac
October 4, 2013

You're welcome. You might find this article helpful as you develop your understanding.