Very odd TimedTask behavior

oripessach

  • *
  • Posts: 259
I'm having a problem with a TimedTask that's set to run every 5 seconds, but occasionally decides to run every 10 ms, instead. I poked around in the engine source and found that the timer member of the TimedTask instance reaches zero, and then -10, -20, etc. etc.

What's more, doExecute() doesn't seem to return to the update() method. I tried modifying TimedTask.hx to catch exceptions around doExecute(),  but that didn't make a difference - doExecute() just seems to somehow quit without returning, although I'm pretty sure that can't (and probably doesn't) actually happen.

Any ideas what's happening here?

rob1221

  • *
  • Posts: 9472
That's weird.  Can you attach a sample game demonstrating the problem?

oripessach

  • *
  • Posts: 259
You're right - that is weird. And not an engine problem, as it turns out. It seems like dereferencing a null object has some unpredictable and amusing effects, and invoking random callbacks is probably the least of your worries if you do that. :)

So this one was a bug in my code - a null reference.

However, while debugging this issue I realized that the part of the engine that deals with TimedTask instances is potentially fragile. It looks as if it can handle new tasks being added in timer callbacks, but if a timer callback ever calls removeTask, bad things will happen. It's usually good practice to break down this kind of operations into two parts: The first loops through the tasks and adds any tasks that need handling into a temporary array, and the second part iterates over the temporary array, calling all the update() methods and removing any tasks that need to be removed.