I’m trying to isolate and fix a rare occurrence where a scheduled task in the agent is not reinstated after the imp device resets. It is likely a bug in my code somewhere, but it would be handy to know if imp.cancelwakeup() kills the timer immediately.
For background, I have a number of scheduled tasks running in the agent and in the device. If the device restarts it sends a restart event, the agent (which holds onto the handles for all its timers) then kills all the timers and then starts new ones almost immediately afterwards. I wondered whether the handle for the newly created timer ends up being the same as the one I just destroyed.
After imp.cancelwakeup() the timer is guaranteed not to be called. The timer handle will continue to exist (uselessly) as long as your code keeps a reference to it anywhere. Once no references exist, it is destroyed, garbage-collected. It’s possible that a future timer handle will get the same value – but you can’t easily tell that, because if you kept the old value to compare it against then that counts as keeping a reference, so the old one won’t have been destroyed.
Thanks Peter, although I’m not sure if my code is totally in the clear here.
If the agent detects that the device has reset it does the following:
It iterates through all instances that have scheduled tasks and kills them with imp.cancelwakeup(). [Would do this in a destructor if one existed in Squirrel]
It then deletes all the instances by deleting them from the table that they occupy using delete myTable[myInstance]. These instances held the only reference to the timer handles.
The agent then reinstates all the instances, all of which will initialise new timers and schedule a task with imp.wakeup().
Because it reinstates the same objects in the same order, I imagine that it’s possible that a handle could be the same as its previous incarnation. If the handle were the same, can I be confident that the prior imp.cancelwakeup() will have no impact on the new timer?
Yes, that’s guaranteed. When you create the new timer, either the old one is dead and forgotten (so its cancelled status is irrelevant), or it’s still live (so its handle can’t be reused).
+1 for for a class destructor