ActionScript 3 Local Timer Object Event Handler

Started by
1 comment, last by Black Knight 13 years ago

I have the following code in a class function :

public function foo():void
{
var timer:Timer = new Timer(10000,1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerComplete);
timer.start();
}

public function onTimerComplete(e:TimerEvent):void
{
// do stuff
}
The above code works most of the time but my concern is what happens if timer gets garbage collected? Is it possible that onTimerComplete will never fire because there are no other references to timer?

For some discussion about this : http://stackoverflow.com/questions/5891354/actionscript-3-local-timer-object-event-handler

Basically I made a test app and created 500 timers and set all their delays to a high value like 60secs and then I spammed garbage collect in my profiler and none of the timers got garbage collected. But after the event handlers ran after 60secs hitting garbage collect freed all of those timers.

I have people saying that removeEventListener must be called on each timer or they wont get released. But this is not what I am observing.

I know timer has an internal list of handlers but that won't keep it from being GC'ed i think.
Advertisement


I have the following code in a class function :

public function foo():void
{
var timer:Timer = new Timer(10000,1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerComplete);
timer.start();
}

public function onTimerComplete(e:TimerEvent):void
{
// do stuff
}
The above code works most of the time but my concern is what happens if timer gets garbage collected? Is it possible that onTimerComplete will never fire because there are no other references to timer?

For some discussion about this : http://stackoverflow...t-event-handler

Basically I made a test app and created 500 timers and set all their delays to a high value like 60secs and then I spammed garbage collect in my profiler and none of the timers got garbage collected. But after the event handlers ran after 60secs hitting garbage collect freed all of those timers.

I have people saying that removeEventListener must be called on each timer or they wont get released. But this is not what I am observing.

I know timer has an internal list of handlers but that won't keep it from being GC'ed i think.




I do this all the time -- and I have never had an instance where it got garbage collected.

You might want to check with another type of listener that isn't just happening once -- like try with the TimerEvent.Timer -- which should fire off with each tick of the clock. If what you are saying is true, it leads me to believe that somehow it knows that the event will only fire off once, and therefore can get rid of it after that.

But in the case of events that might be called multiple times, I am thinking that you'd need to remove the event listener.

Try out with the regular timer event, and let me know -- i'm interested now :)
I will try it with the usual timer event and set repeat count to 5 and then run it and see if they are GC'ed after the run 5 times. I will try it without removeEventListener first then with removeEventListener.

This topic is closed to new replies.

Advertisement