Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Guest
Oct 04, 2013 Oct 04, 2013

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!

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Oct 04, 2013 Oct 04, 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

          

...
Translate
Guide ,
Oct 04, 2013 Oct 04, 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;

     }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 04, 2013 Oct 04, 2013

Thank you Amy!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 04, 2013 Oct 04, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 06, 2013 Oct 06, 2013

Make 'em, and remove 'em.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 06, 2013 Oct 06, 2013

Well it's certainly a good practice to null out and remove listeners, but this line:

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

Is not very problematic. Even though the code is executed from the main timeline (aka 'root') it will create a reference from mc1 to 'root', but not a reference from 'root' to mc1. What this means is that once mc1 is removed (by removing MyClip and nulling the reference to MyClip) then both mc1 and the listener from mc1 to 'root' will be eligable for garbage collection.

By contrast, this is the sort of line that usually gets people in trouble:

// in MyClip somewhere

stage.addEventListener(...)

In this case you have created a reference from the stage to MyClip. Now even if you remove and null out MyClip, the stage still has a reference (via listener) to MyClip so it will never be eligable for GC (unless that listener was added as a weak listener -- not a bad practice IMO when adding stage listeners.)

- Aaron

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 07, 2013 Oct 07, 2013

Aaron--that's what I said. The issue is that it is still problematic because the listener will still continue to fire until the object is garbage collected, and you can't control when that happens.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 07, 2013 Oct 07, 2013

Fair enough, I didn't quite follow your example. In this case there's almost no way a click handler could fire on an object that isn't on the display list, since it's impossible to click it.

-Aaron

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 07, 2013 Oct 07, 2013
LATEST

Good point. I guess I read the question a bit more generally.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines