Storyboard: storyboard.removeScene() and Timers & Transitions

When a module is unloaded via storyboard.removeScene() is it necessary to have canceled and nil’d out completed timers and transitions?

--1.lua--  
local function test ()  
 print "test"  
storyboard.gotoScene( "2", "slideLeft" )  
end  
local timer1 = timer.performWithDelay(1000, test, 1)  
  
--2.lua--  
storyboard.removeScene(1)  

[import]uid: 73951 topic_id: 28461 reply_id: 328461[/import]

My guess is no, but there’s a roundabout way to check…

  1. Try running this in main.lua
  2. Comment out any storyboard.gotoScene commands so it actually runs :wink:

local timer1 = timer.performWithDelay(1000, function() print("test") end) local timer2 = timer.performWithDelay(1500, function() print("timer1 = ", timer1) end)

If you see “timer1 = nil” then your completed timers/transitions are safe.
If you see "timer1 = table "something something then you’ll need to nil it out on exit.

[import]uid: 41884 topic_id: 28461 reply_id: 114948[/import]

@richard9 Thanks. I did that and the timer still existed. I’d assume i’d have to nil it out.


Here are a few other questions that may clear this annoying situation up (this goes for timers with or without handles):
When a timer has finished does it ever automatically nil itself out? (In other words, do completed timers automatically become eligible for garbage collection?)

Must one do a timer.cancel() on a timer that has finished prior to nil’ing it out? [My guess is that timer.cancel() is only meant for currently running timers (with time remaining), but I want to be absolutely sure.]

Finally, must one do a timer.pause() prior to timer.cancel() (assuming a timer is in progress/has time remaining) ?

[import]uid: 73951 topic_id: 28461 reply_id: 115069[/import]

Any ideas? This is really bugging me. [import]uid: 73951 topic_id: 28461 reply_id: 115225[/import]

I wonder if Tom @CoronaLabs would do a FAQ session all about timers (and another one about transitions) as it relates to dealing with their handles and memory management.

Anyhow, I am assuming that the fired timers can’t be canceled, and once fired, the handle to the fired timer will become nil. But I could be wrong.

Naomi [import]uid: 67217 topic_id: 28461 reply_id: 115233[/import]

I agree. I just don’t want to leave anything “behind” that I shouldn’t. It could add up very quickly! I just want to get a “handle” on the situation. I read your thread about the issues and I’m glad I was able to bring it back to the surface. I’m also glad that I’m not the only one worried about these things. I’m trying to distill my code down to the point where I’m only using the tools/methods I need. Besides, simpler code is easier to manage later on. [import]uid: 73951 topic_id: 28461 reply_id: 115234[/import]

Oh, I can answer a few of those questions from personal experience:

  1. No, you don’t have to pause an active timer first. Pretty much only need to pause if you need to preserve the remaining time for some reason.

  2. No, you don’t have to cancel an expired timer. When the timer is done it just becomes an inert table. In fact, I’m guessing you could get away with something like this:

object.wait = timer.performWithDelay(500, function() print("hi!"); object.wait = nil end)

“When a timer (or transition) has finished does it ever automatically nil itself out? (In other words, do completed timers automatically become eligible for garbage collection?)”

What’s the case with this?

Must one manually nil out a completed or cancelled timer or transition?

Is it ever done automatically?

[import]uid: 73951 topic_id: 28461 reply_id: 117426[/import]

Anyone have any ideas? I really need to know if I’m doing any unnecessary extra work. [import]uid: 73951 topic_id: 28461 reply_id: 119896[/import]

Well unless somebody speaks up this is the best route:

  1. You don’t cancel finished timers
  1. You do nil out finished timers [import]uid: 41884 topic_id: 28461 reply_id: 119910[/import]

Thanks! I appreciate your help. Those seem like good guidelines to follow. Sorry for being so picky/insistent. That’ll do for now. I’ll just wait for further clarification, if necessary! [import]uid: 73951 topic_id: 28461 reply_id: 119912[/import]

Anyone have any ideas? I really need to know if I’m doing any unnecessary extra work. [import]uid: 73951 topic_id: 28461 reply_id: 119896[/import]

Well unless somebody speaks up this is the best route:

  1. You don’t cancel finished timers
  1. You do nil out finished timers [import]uid: 41884 topic_id: 28461 reply_id: 119910[/import]

Thanks! I appreciate your help. Those seem like good guidelines to follow. Sorry for being so picky/insistent. That’ll do for now. I’ll just wait for further clarification, if necessary! [import]uid: 73951 topic_id: 28461 reply_id: 119912[/import]