Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

ActionScript 3 Local Timer Object Event Handler

This topic is 2694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!