When exiting a scene using the storyboard API, I cancel all the transitions using the technique described in http://www.coronalabs.com/blog/2011/08/15/corona-sdk-memory-leak-prevention-101/. Unfortunately, when there are more than a few transitions (it happens when creating more than one objects), several of them remain in memory and continue calling the onComplete functions. That is, some transitions are cancelled and others are not. This problem may be related to the way my variables are scoped and I’m hoping someone else can help spot the error in my reasoning.
The overview of the code is as such: a build function parses a table and checks the keys to determine what object creation function to call. For simplicity, only the problematic function is shown here. The object in the createMover function is built using the arguments passed in from the build function. During the creation of the object, transitions are created and added to a global transitionStash table. Basically, the ‘box’ moves back and forth, no sweat. When the scene exits, the cancelAllTransitions function is called. Looping through the transitionStash immediately after it has been cancelled reveals an empty table as expected; however, looping through it on the enterScene event for the following scene reveals several transitions that still exist. The transition continues after being cancelled when the object is returned (or not), when it is added to a table in the build function (or not), when that table in the build function is returned (or not). So, my usual tricks for checking scope are unable to resolve this issue. Please have a look and point out the error of my ways. 
[blockcode]
function scene:createScene(event)
group = self.view
staticGroup = display.newGroup()
group:insert(staticGroup)
local function createMover (x, y, x0, y0, xF, yF, v)
local box = display.newImageRect(“images/ice.png”, 64, 64)
box.x = x * unit + unit/2
box.uID = x * y
–Additional declarations redacted for clarity
local setup, goToEnd, goToStart
setup = function()
transitionStash[tostring(box.uID)] = transition.to(box, {time=box.offset*1000/box.speed, x = box.xF, y = box.yF, onComplete = goToStart})
end
goToEnd = function()
transitionStash[tostring(box.uID)] = transition.to(box, {time=box.distance*1000/box.speed, x = box.xF, y = box.yF, onComplete = goToStart})
end
goToStart = function()
transitionStash[tostring(box.uID)] = transition.to(box, {time=box.distance*1000/box.speed, x = box.x0, y = box.y0, onComplete = goToEnd})
end
setup()
return box
end
local function build(levelObject)
–[[ Pass a level table into this function. The table is iterated
for each type of drawn element and associated function ]]–
local movers = {}
for key,value in pairs(levelObject) do
if (key == “movingBoxes”) then
for _, val in ipairs(levelObject.movingBoxes) do
–Tried to add to table to no effect
table.insert(movers, createMover(val.x, val.y, val.x0, val.y0, val.xF, val.yF, 2))
staticGroup:insert(movers[#movers])
end
elseif (key == “pushBoxes”) then
for _, val in ipairs(levelObject.pushBoxes) do
–Works fine for transitionless objects if the creating function returns nothing here
createPushable(val.x, val.y)
end
end
end
end
end
function scene:exitScene(event)
local group = self.view
cancelAllTransitions()
cancelAllTimers()
end
[/blockcode]
It doesn’t matter if the box object is added to the staticGroup in the createMover function or from the table in the build function. Is it necessary to return the ‘box’ object and add it into a table in the build function? All the other object creating functions do not return the object and work fine.
local function createPushable (x, y)
local push = display.newImageRect("images/ball.png", 64, 64)
push.x = x \* unit + unit/2
push.y = y \* unit + unit/2
push.direction = 4
push.myName = "sliding"
staticGroup:insert(push)
end
Just for comfort, here’s the cancelAllTransitions function
[blockcode]
– GLOBAL DECLARATIONS (from main.lua)–
transitionStash = {}
function cancelAllTransitions()
local k, v
for k,v in pairs(transitionStash) do
transition.cancel( v )
v = nil; k = nil
end
transitionStash = nil
transitionStash = {}
end
[/blockcode]
The other objects created using similar functions to createMover (without transitions) get garbage collected correctly. Memory usage at the menu scene is always within 1k after a level except when the scene contained more than a few of these objects with transitions. Thank you in advance for helping fix this code! [import]uid: 168249 topic_id: 30194 reply_id: 330194[/import]
