Copy link to clipboard
Copied
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!
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
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
Thank you Amy!
Copy link to clipboard
Copied
You're welcome. You might find this article helpful as you develop your understanding.
Copy link to clipboard
Copied
Make 'em, and remove 'em.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Good point. I guess I read the question a bit more generally.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now