Josh Brittain
Josh Brittain

Reputation: 2212

Why isn't this garbage collected

If I make a timer such as

var timer:Timer = new Timer(50, 0);
timer.addEventListener(TimerEvent.TIMER, OnTimer);
timer.start();

and then my function ends, you would think this timer has gone out of scope and nothing is holding on to a reference of it anymore. However this timer still works.

So either I am getting lucky and the garbage collector hasn't run yet or something is holding on to a reference. If it is the latter then how will I know it is going to be garbage collected?

Upvotes: 1

Views: 55

Answers (1)

sybear
sybear

Reputation: 7784

Timer will still run and will be dispatching events. Just declare it the way you can access it (as public instance variable) and perform:

  • timer.stop();
  • timer.removeEventListener(TimerEvent.TIMER, OnTimer); - VERY important thing in Flash
  • timer = null; - if you really need to free memory, set the reference to null

Upvotes: 2

Related Questions