动作3:你需要删除EventListeners的? [英] Actionscript 3: Do you need to remove EventListeners?

查看:146
本文介绍了动作3:你需要删除EventListeners的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ActionScript 3中,我动态创建,而我添加EventListeners的对象。这些对象被添加到阵列和再购买可能会被删除。而其他人可能会稍后再添加。每次我创建一个对象,我添加这些EventListeners的给他们。但是,是否有必要删除这些对象时删除这些事件侦听器也?当我失去了所有引用的对象,但不删除这些EventListeners的,会发生什么?难道他们留在某个地方的内存,无法连接和unusuable,还是在GC清理它们?

In actionscript 3, I dynamically create objects to which I add EventListeners. These objects are added to arrays and might be removed again later. And others might be added later again. Each time I create an object, I add these EventListeners to them. However, is it necessary to remove these event listeners too, when deleting these objects? What happens when I lose all references to an object but don't delete these EventListeners? Do they stay somewhere in the memory, unreachable and unusuable, or does the GC clean them up?

推荐答案

是的,你如果不使用弱引用删除事件侦听器。 GC不会清理的对象,如果有对它的引用,并注册事件侦听器做的,除非你设置了将useWeakReference 参数创建一个参考对象(5 < SUP>日参数的<一个href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/EventDispatcher.html#addEventListener%28%29"相对=nofollow> 的addEventListener 法),同时注册了事件侦听器。弱引用将不会被垃圾收集器进行计数。

Yes, you have to remove event listeners if you are not using weak references. GC won't clean up an object if there is a reference to it, and registering event listeners do create a reference to the object unless you set the useWeakReference parameter (the 5th parameter to the addEventListener method) to true while registering the event listener. Weak references won't be counted by the garbage collector.

//Using strong reference: needs to be removed by calling removeEventListener
sprite.addEventListener(Event.TYPE, listenerFunction, useCaptureBool, 0, false);

//Using a weak reference: no need to call removeEventListener
sprite.addEventListener(Event.TYPE, listenerFunction, useCaptureBool, 0, true);

这篇关于动作3:你需要删除EventListeners的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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