Hi,
I try to develop a rythmic game and i get a memory leak with the approach i use.
I try to shoot some bullets that follow the beat of a song, here’s my code:
local storyboard = require("storyboard") local scene = storyboard.newScene(); storyboard.isDebug = true storyboard.purgeOnSceneChange = true --Calling all the local variables local backgroundGame2 local scoreText local score = 0 local timing = {} local Bullet = {} local anim = {} local tmr2 local tmr3 local Shoot -- I use this table to shoot my bullets at the right time using transition.to with a delay timing = {500, 2500, 4500, 6500, 8500, 10500, 12500, 14500} function scene:createScene(e) local view = self.view backgroundGame2 = display.newRect(\_W/2, \_H/2, \_W, \_H) backgroundGame2:setFillColor(255,255,255) view:insert(backgroundGame2) scoreText = display.newText(score, \_W/2, 380, "HelveticaNeue", 40); scoreText:setFillColor(0.95, 0.83, 0); function scoreText:tap(e) --I just want to look at my memory usage so I use the score to return to the previous scene storyboard.gotoScene("menu",{ effect = "slideLeft", time = "250" }) end scoreText:addEventListener("tap",scoreText); view:insert(scoreText) function Shoot () --This function creates #timing Bullets that cross the screen one by one using animation and delay for i = 1, #timing do Bullet[i] = display.newImageRect("Bullet.png", 40,30,20) Bullet[i].x = \_W/2 -130; Bullet[i].y = \_H/2 +70; anim[i] = transition.to(Bullet[i],{ --Pre-Launch time = 300, delay = timing[i], x = 40, onComplete = function() anim[i] = transition.to(Bullet[i],{ --Launch time = 1500, delay = 1000, x = 290, onComplete = function() transition.cancel(anim[i]); display.remove(Bullet[i]) Bullet[i]=nil anim[i]=nil score = score + 1 scoreText.text = score end }) end }) view:insert(Bullet[i]) end end function scene:enterScene(e) storyboard.printMemUsage() tmr2 = timer.performWithDelay(500,Shoot) tmr3 = timer.performWithDelay(25500, Shoot) end function scene:exitScene(e) --Cancel and nil the timers if tmr2 ~= nil then timer.cancel(tmr2) end tmr2 = nil if tmr3 ~= nil then timer.cancel(tmr3) end tmr3 = nil --Remove and nil my images and animations for i = 1, #Bullet do if (Bullet[i] ~= nil) then display.remove(Bullet[i]) Bullet[i] = nil end if (anim[i] ~= nil) then transition.cancel(anim) anim[i] = nil end end end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) return scene;
My memory usage keeps increasing even if I remove.Scene when i get out of this one (just like in this vid: https://www.youtube.com/watch?v=2IDzu6qWRCM )
The leak is bigger at the beginning of the function Shoot. When all the animations are over, there’s no memory leak.
Where is my leak or do you have another approach?
Thanks,
Leo