就在动作监听器弱引用澄清监听器、就在、动作

2023-09-08 12:59:41 作者:权志龙,VIP们等你

我知道如何引用工作薄弱,但我有点糊涂了关于它在动作事件监听器使用。考虑下面的例子:

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");        
  }        
}

现在在这里,因为只有一个弱引用富,不会对事件侦听器的Foo被垃圾收集如果当垃圾收集器运行和code停止按预期工作?

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?

时仅作下同类中的事件侦听器方法的薄弱事件侦听器的情况prescribed?

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?

如果矩形对象没有其他引用,那么它就是垃圾回收的候选人,但由于在对象中的事件侦听器,该事件调度持有对象的引用,即使有任何其它引用对象(比由事件侦听器保持的一个其它)。因此,它是被垃圾收集pvented $ P $。这就是为什么弱事件侦听器prescribed的原因是什么? Flash Player的天真的,不能计算出该事件监听器相同的对象中定义的?

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?

推荐答案

在Flash垃圾收集的基本规则是,如果一个对象引用(直接或间接)从时间线上,它不能被垃圾收集。这意味着,如果没有正在从任何地方引用的根显示列表中的类的一个实例(或由一个对象,它是依次从根引用,等Ç递归的),它将被不管是否有对收集本身。

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.

所以,在你的第一个例子中,实例是在所有情况下进行垃圾回收。在你的第二个,不管是不是在矩形实例可以被垃圾收集取决于无论是从显示列表中引用。该将useWeakReference 标志在这方面没有什么区别。

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.

从文档这里:

类级别成员函数不属于垃圾回收,这样你就可以useWeakReference设置为true的类级别成员函数没有对他们进行垃圾回收。如果您useWeakReference设置为true的侦听器,这是一个嵌套的内部函数,该函数将作为垃圾回收并且不再是永久。如果创建引用内部函数(保存在另一个变量),那么它是不是垃圾回收并仍将保持永久。

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.