Flash垃圾收集器和处理事件
看看下面的代码,
var a = new View();
a = null;
....
class View {
private var clip: MovieCLip
public function View() {
clip.addEventListener(...)
}
}
a
在a = null
之后会在内存中吗? addEventListener
是否添加了强引用?
take a look at the following code
var a = new View();
a = null;
....
class View {
private var clip: MovieCLip
public function View() {
clip.addEventListener(...)
}
}
will a
be in memory after a = null
? Does addEventListener
adds a strong refernce?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
addEventListener
添加强引用。 addEventListener 的最后一个参数 是useWeakReference
。您可以对此参数使用true
来指定弱引用。By default
addEventListener
adds a strong reference. The last parameter of addEventListener isuseWeakReference
. You can usetrue
for this parameter to specify a weak reference.正如您所描述的示例一样,附加事件侦听器的对象将不会被垃圾收集。即使设置为 null 也无济于事。
要获取此对象 goto gc(),您可以使用以下方法之一:
useWeakReference
clip.addEventListener(EVENT.name,listenerMethod,false,0,true);
取消订阅侦听器。
在处理程序方法中
As you describe your example, the object where the event listener is attached will not be garbage collected. Even setting null will not help.
TO get this object goto gc() you can use one of the following approaches:
useWeakReference
clip.addEventListener(EVENT.name,listenerMethod,false,0,true);
unsubscribe listener.
In handler method
由于对
clip
的所有引用都在a
内,GC 将拾取这两个对象并彻底删除它们。我采用了您的示例,并使用 ENTER_FRAME 侦听器以与您相同的方式创建新的
View
:但是,如果剪辑被添加到舞台,那么它将继续存在,并且
a
也不会被删除:您可以使用
addEventListener
的useWeakReference
参数> 以防止这种情况发生。Since all the references to
clip
are withina
, GC will pick up both objects and cleanly remove them.I've taken your example and used an ENTER_FRAME listener to create new
View
s in the same way you did:If, however, clip were added to the stage, then it would continue to exist, and
a
would also not be removed:You can use the
useWeakReference
parameter ofaddEventListener
to prevent this from happening.