对 ActionScript 3 中的垃圾收集和弱引用事件感到困惑
我有一个对象的引用。 该对象有一个带有弱引用的计时器事件。 示例:
timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
现在我删除了这个引用(test 是包含该引用的变量):
test = null;
然而,timerHandler 仍然被触发。 这是不可能的吗,所以我的代码中一定有某种错误,没有其他可能性吗?
或者这确实不应该阻止计时器功能一直运行?
I have a reference to an object. This object has a timer event with a weak reference. Example:
timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
Now I remove this reference (test is the variable containing the reference):
test = null;
And yet, the timerHandler keeps being fired. Is this impossible, so that I must have somekind of mistake in my code, without any other possibility?
Or is this indeed not supposed to stop the timer function from being run all the time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
垃圾收集器不会持续运行,因此很可能它还没有运行。 当它最终完成时,您的处理程序应该停止被调用。 如果没有,可能还有另一个参考。
当我运行下面的示例时,我看到
timer
无限期地跟踪,即使handler
已设置为 null 并且EventDispatcher
具有弱引用。 但是,如果我通过取消注释 System.gc() 行(使用调试播放器)来强制垃圾收集器运行,则永远不会调用处理程序。一般来说,您不应该依赖垃圾收集器来保证程序的正确运行。
The garbage collector doesn't operate continually, so it's likely that it just hasn't run yet. When it finally does, your handler should stop being called. If not, there is probably another reference to it.
When I run the example below, I see
timer
traced indefinitely, even thoughhandler
has been set to null and theEventDispatcher
has a weak reference. However, if I force the garbage collector to run by uncommenting theSystem.gc()
line (using the debug player), the handler is never called.In general, you shouldn't rely on the garbage collector for the correct operation of your program.