AS3:弱监听器引用不宜在初始化过程? [英] AS3: Weak Listener References Not Appropriate During Initialization?

查看:217
本文介绍了AS3:弱监听器引用不宜在初始化过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在明白了,如果事件监听器被添加到一个对象,将useWeakReference设置为true,那么它是符合垃圾收集,如果当垃圾回收做了扫描将被删除。

as i currently understand, if an event listener is added to an object with useWeakReference set to true, then it is eligible for garbage collection and will be removed if and when the garbage collection does a sweep.

public function myCustomSpriteClass() //constructor
    {
    this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener, false, 0, true);
    this.addEventListener(MouseEvent.MOUSE_UP, mouseUpListener, false, 0, true);
    }

在这种情况下,它是不恰当初始化对象用弱引用的事件监听器,柜面因为他们的对象的初始化期间加入垃圾回收器激活一个扫除去对象事件侦听器?

in this case, is it not appropriate to initialize an object with weak references event listeners, incase the garbage collector does activate a sweep removing the objects event listeners since they were added during initialization of the object?

在这种情况下,将它才适宜创建类型的解除分配()方法,该方法取消了事件侦听器对象被无效之前?

in this case, would it only be appropriate to create a type of deallocate() method which removes the event listeners before the object is nullified?

推荐答案

弱事件侦听器只意味着听众在垃圾收集程序,如不计算在内。如果对象没有其它指针,但强EventListeners的,它不会被通过GC收集,如果它只有弱引用那么它将被删除。

weak event listeners only means that the listeners are not counted in the garbage collection routine, eg. if an object has no other pointers but strong eventlisteners, it will not be collected by GC, if it only has weak references then it will be removed.

在事件监听器本身不被GC删除,你必须删除它们以同样的方式,如果他们是强还是弱,如果对象是无效但是弱引用监听器应该被自动丢弃。

the event listeners themselves are not removed by GC, you have to remove them in the same way if they are weak or strong, however weak referenced listeners should automatically be trashed if the object is nullified.

我个人认为,使用弱监听促进陋习,你不再有实际想想,你的资源使用,尽管它们在某些情况下非常有用。我想有一个清理脚本来剥夺其您注销之前运行监听它。 虽然有传福音双方(他可能会解释更好,如果你还在迷茫)

personally i think that the use of weak listeners promotes bad practices as you no longer have to actually think about what resources you are using, although they are useful in certain situations. I would have a clean-up script to strip it of its listeners that you run before nullification. although there are evangelists for both sides (and he might explain it better if you are still confused)

-edited,使更多的透明感 -

-edited to make more clear sense-

这篇关于AS3:弱监听器引用不宜在初始化过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆