关于动作脚本侦听器中弱引用的澄清

发布于 2024-12-01 16:47:53 字数 1071 浏览 0 评论 0原文

我理解弱引用是如何工作的,但我对它在动作脚本事件监听器中的使用有点困惑。考虑下面的例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风轻花落早 2024-12-08 16:47:53

Flash 中垃圾收集的基本规则是,如果从时间轴引用(直接或间接)对象,则无法对其进行垃圾收集。这意味着,如果您的类的实例没有从根显示列表中的任何位置引用(或者没有被从根显示列表中依次引用的对象等递归引用),则无论它是否引用了本身。

这也意味着相互引用但未以任何方式从根显示列表引用的两个对象应该符合垃圾回收的条件。

事件监听器中的弱引用选项主要是为了让您不必手动删除事件监听器。我个人倾向于不使用它,因为我喜欢完全控制对象何时被标记为垃圾回收。

因此,在第一个示例中, Foo 实例在所有情况下都符合垃圾回收条件。在第二个例子中,Rectangle 实例是否可以被垃圾收集取决于它是否从显示列表中引用。在这方面,useWeakReference 标志没有什么区别。

从文档此处< /a>:

类级别的成员函数不受垃圾回收的影响,因此您可以将类级别的成员函数的 useWeakReference 设置为 true,而不会使它们受到垃圾回收的影响。如果将嵌套内部函数的侦听器的 useWeakReference 设置为 true,则该函数将被垃圾收集并且不再持久。如果您创建对内部函数的引用(将其保存在另一个变量中),那么它不会被垃圾收集并保持持久性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文