关于动作脚本侦听器中弱引用的澄清
我理解弱引用是如何工作的,但我对它在动作脚本事件监听器中的使用有点困惑。考虑下面的例子:
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");
}
}
现在,由于只有对 Foo 的弱引用,如果垃圾收集器运行并且代码按预期停止工作,事件侦听器 Foo 不会被垃圾收集吗?
弱事件侦听器场景是否仅针对同一类中的事件侦听器方法规定,如下所示?
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");
}
}
在上面的场景中,弱事件监听器就是这样提供帮助的吗?
如果 Rectangle 对象没有其他引用,则它是垃圾回收的候选对象,但由于该对象内有一个事件侦听器,因此事件调度程序保留对该对象的引用,即使没有对该对象的其他引用(除了事件侦听器持有的事件之外)。因此可以防止它被垃圾收集。这就是规定弱事件监听器的原因吗? Flash 播放器是否如此天真以至于无法弄清楚事件侦听器是在同一个对象中定义的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Flash 中垃圾收集的基本规则是,如果从时间轴引用(直接或间接)对象,则无法对其进行垃圾收集。这意味着,如果您的类的实例没有从根显示列表中的任何位置引用(或者没有被从根显示列表中依次引用的对象等递归引用),则无论它是否引用了本身。
这也意味着相互引用但未以任何方式从根显示列表引用的两个对象应该符合垃圾回收的条件。
事件监听器中的弱引用选项主要是为了让您不必手动删除事件监听器。我个人倾向于不使用它,因为我喜欢完全控制对象何时被标记为垃圾回收。
因此,在第一个示例中,
Foo
实例在所有情况下都符合垃圾回收条件。在第二个例子中,Rectangle
实例是否可以被垃圾收集取决于它是否从显示列表中引用。在这方面,useWeakReference
标志没有什么区别。从文档此处< /a>:
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 theRectangle
instance can be garbage collected depends on whether it's referenced from the display list. TheuseWeakReference
flag in this regard makes no difference.From the documentation here: