Memory leak problem

Hi…

I’m having a memory leak which I can’t figure out-

I’m creating in my app objects that are added into a table, and once they go off screen or captured by the player, I remove them.

even so, the memory keeps adding up to the point where the game is unplayable…

Can someone please check me code and tell me what am I missing?

function newCrate()

–[[crate criteria]]

    local crate=display.newImage(“Photos/crate.png”)

    table.insert(t,crate)

    crate.width = 50

    crate.height= 50

    crate.y=-20

    crate.x= math.random(50,900)

    crate.rotation=math.random(0,350)

    physics.addBody(crate, “dynamic”, {bounce=0.5})

    crate.linearDamping =math.random(0.5,2.5)

    local transCrate=transition.to( crate, {time=5000,rotation=0} )

    crate.myName="crate"

    --[[check if object to be killed]]

    local function reventar(event)

    if(event.object2.class==“ball”) then

        event.object1.fastkill = true

    elseif(event.object1.class==“ball”) then

        event.object2.fastkill=true

    end

    end

    --[[object handler]]

    function suck(obj)

        ball.ballX(obj)

        local bx=obj.x

        local by=obj.y

        objy = display.contentWidth * 0.5+35

        local trans=transition.to(obj, {time=250, width=0,height=0,x=obj.x, y=obj.y,alpha=0})

    end

local function loop(event)

         for i = #t, 1, -1 do

            local object = t[i]

                    if(object.y~=nil) then

                        if object.y>700 then

                        score.setScore(-20)

                        physics.removeBody(object)

                        transition.cancel(transCrate)

                        local child = table.remove(t, i) 

                                if child ~= nil then

                                child:removeSelf()

                                child = nil

                                collectgarbage()

                                    if(trans~=nil) then

                                    transition.cancel(trans)

                                    collectgarbage()

                                    end

                                end

                          end

                     end

                if object.fastkill then

                ball.addOne()

                score.setScore(100)

                score.showGood(object,object.x,object.y)

                suck(object)

                    local child = table.remove(t, i)    

                            timer.performWithDelay(300,function()

                                if(child~=nil) then

                                    child:removeSelf()

                                    child = nil

                                    collectgarbage()

                                end

                            end)  

                end

            end

        end

    for i=1,#t do

        t[i].id = i

    end

Runtime:addEventListener(“enterFrame”, loop)

Runtime:addEventListener(“collision”,reventar)

end

You’re adding 2 runtime listeners every time you call the newCrate function.  Thats your memory leak.  Change it to this (done in a hurry):

local transCrate function newCrate() --[[crate criteria]] local crate=display.newRect(10,10,10,10) table.insert(t,crate) crate.width = 50 crate.height= 50 crate.y=-20 crate.x= math.random(50,900) crate.rotation=math.random(0,350) physics.addBody(crate, "dynamic", {bounce=0.5}) crate.linearDamping =math.random(0.5,2.5) transCrate=transition.to( crate, {time=5000,rotation=0} ) crate.myName="crate" --[[check if object to be killed]] end local function reventar(event) if(event.object2.class=="ball") then event.object1.fastkill = true elseif(event.object1.class=="ball") then event.object2.fastkill=true end end --[[object handler]] function suck(obj) ball.ballX(obj) local bx=obj.x local by=obj.y objy = display.contentWidth \* 0.5+35 local trans=transition.to(obj, {time=250, width=0,height=0,x=obj.x, y=obj.y,alpha=0}) end local function loop(event) for i = #t, 1, -1 do local object = t[i] if(object.y~=nil) then if object.y\>700 then score.setScore(-20) physics.removeBody(object) transition.cancel(transCrate) local child = table.remove(t, i) if child ~= nil then child:removeSelf() child = nil collectgarbage() if(trans~=nil) then transition.cancel(trans) collectgarbage() end end end end if object.fastkill then ball.addOne() score.setScore(100) score.showGood(object,object.x,object.y) suck(object) local child = table.remove(t, i) timer.performWithDelay(300,function() if(child~=nil) then child:removeSelf() child = nil collectgarbage() end end) end end end for i=1,#t do t[i].id = i end Runtime:addEventListener("enterFrame", loop) Runtime:addEventListener("collision",reventar)

dunno how i missed it…

thanks :slight_smile:

You’re adding 2 runtime listeners every time you call the newCrate function.  Thats your memory leak.  Change it to this (done in a hurry):

local transCrate function newCrate() --[[crate criteria]] local crate=display.newRect(10,10,10,10) table.insert(t,crate) crate.width = 50 crate.height= 50 crate.y=-20 crate.x= math.random(50,900) crate.rotation=math.random(0,350) physics.addBody(crate, "dynamic", {bounce=0.5}) crate.linearDamping =math.random(0.5,2.5) transCrate=transition.to( crate, {time=5000,rotation=0} ) crate.myName="crate" --[[check if object to be killed]] end local function reventar(event) if(event.object2.class=="ball") then event.object1.fastkill = true elseif(event.object1.class=="ball") then event.object2.fastkill=true end end --[[object handler]] function suck(obj) ball.ballX(obj) local bx=obj.x local by=obj.y objy = display.contentWidth \* 0.5+35 local trans=transition.to(obj, {time=250, width=0,height=0,x=obj.x, y=obj.y,alpha=0}) end local function loop(event) for i = #t, 1, -1 do local object = t[i] if(object.y~=nil) then if object.y\>700 then score.setScore(-20) physics.removeBody(object) transition.cancel(transCrate) local child = table.remove(t, i) if child ~= nil then child:removeSelf() child = nil collectgarbage() if(trans~=nil) then transition.cancel(trans) collectgarbage() end end end end if object.fastkill then ball.addOne() score.setScore(100) score.showGood(object,object.x,object.y) suck(object) local child = table.remove(t, i) timer.performWithDelay(300,function() if(child~=nil) then child:removeSelf() child = nil collectgarbage() end end) end end end for i=1,#t do t[i].id = i end Runtime:addEventListener("enterFrame", loop) Runtime:addEventListener("collision",reventar)

dunno how i missed it…

thanks :slight_smile: