问题
I understand how weak references work, but I am bit confused regarding it's use in actionscript event listeners. Consider the example below:
public class Rectangle extends MovieClip {
public function Rectangle() {
var screen:Shape=new Shape();
screen.addEventListener(MouseEvent.MOUSE_OUT, new Foo().listen, false, 0, true);
addChild(screen);
}
}
public class Foo extends MovieClip {
public function listen(e:MouseEvent):void {
trace("tracing");
}
}
Now here, since there is only a weak reference to Foo, would not the event listener Foo be garbage collected if and when the garbage collector runs and the code stop working as expected?
Is the weak event listener scenario prescribed only for event listener methods within the same class as below?
public class Rectangle extends MovieClip {
public function Rectangle() {
var screen:Shape=new Shape();
screen..addEventListener(MouseEvent.MOUSE_OUT, listen, false, 0, true);
addChild(screen);
}
public function listen(e:MouseEvent):void {
trace("tracing");
}
}
In the above scenario, is this how weak event listeners help?
If the Rectangle object has no other references, then it is a candidate for garbage collection, but since there is an event listener within the object, the event dispatcher holds a reference to the object, even though there are no other references to the object(other than the one held by the event listener). Hence it is prevented from being garbage collected. Is this the reason why weak event listeners are prescribed? Is the flash player so naive that, it cannot figure out that the event listener is defined within the same object?
回答1:
The basic rule for garbage collection in Flash is that if an object is referenced (directly or indirectly) from the timeline, it cannot be garbage collected. This means that if an instance of your class is not being referenced from anywhere in the root display list (or by an object that is in turn referenced from the root, et c recursively), it will be collected regardless of whether it has references to itself.
This also means that two objects that reference each other, but are not being referenced in any way from the root display list, should be eligible for garbage collection.
The weak reference option in event listeners is mainly so that you won't have to remove the event listener manually. I personally tend not to use it, because I like to have full control of when objects get marked for garbage collection.
So, in your first example, the Foo
instance is in all cases eligible for garbage collection. In your second, whether or not the Rectangle
instance can be garbage collected depends on whether it's referenced from the display list. The useWeakReference
flag in this regard makes no difference.
From the documentation here:
Class-level member functions are not subject to garbage collection, so you can set useWeakReference to true for class-level member functions without subjecting them to garbage collection. If you set useWeakReference to true for a listener that is a nested inner function, the function will be garbage-collected and no longer persistent. If you create references to the inner function (save it in another variable) then it is not garbage-collected and stays persistent.
来源:https://stackoverflow.com/questions/7231636/clarifications-regarding-weak-references-in-actionscript-listeners