对 ActionScript 3 中的垃圾收集和弱引用事件感到困惑

发布于 2024-08-01 21:16:38 字数 314 浏览 1 评论 0原文

我有一个对象的引用。 该对象有一个带有弱引用的计时器事件。 示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2024-08-08 21:16:38

垃圾收集器不会持续运行,因此很可能它还没有运行。 当它最终完成时,您的处理程序应该停止被调用。 如果没有,可能还有另一个参考。

当我运行下面的示例时,我看到 timer 无限期地跟踪,即使 handler 已设置为 null 并且 EventDispatcher 具有弱引用。 但是,如果我通过取消注释 System.gc() 行(使用调试播放器)来强制垃圾收集器运行,则永远不会调用处理程序。

package {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.TimerEvent;
  import flash.system.System;
  import flash.utils.Timer;

  public class TimerTest extends Sprite {
    private var timer:Timer;
    public function TimerTest() {
      var handler:Function = createHandler();
      timer = new Timer(1000);
      timer.addEventListener(TimerEvent.TIMER, handler, false, 0, true);
      timer.start();
      handler = null;
      //System.gc();
    }

    private function createHandler():Function {
      return function(e:Event):void {
        trace('timer');
      };
    }
  }
}

一般来说,您不应该依赖垃圾收集器来保证程序的正确运行。

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 though handler has been set to null and the EventDispatcher has a weak reference. However, if I force the garbage collector to run by uncommenting the System.gc() line (using the debug player), the handler is never called.

package {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.TimerEvent;
  import flash.system.System;
  import flash.utils.Timer;

  public class TimerTest extends Sprite {
    private var timer:Timer;
    public function TimerTest() {
      var handler:Function = createHandler();
      timer = new Timer(1000);
      timer.addEventListener(TimerEvent.TIMER, handler, false, 0, true);
      timer.start();
      handler = null;
      //System.gc();
    }

    private function createHandler():Function {
      return function(e:Event):void {
        trace('timer');
      };
    }
  }
}

In general, you shouldn't rely on the garbage collector for the correct operation of your program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文