Hi,
I spawn objects and clean up as below but there seem to be memory leak.
local function spawnBomb()
local bomb = display.newImage("bomb.png")
bombs[bomb] = bomb
bombs[bomb].x = math.random(\_W/2, \_W/2)
bombs[bomb].name = "bomb"
physics.addBody(bombs[bomb], {bounce=0})
bombs[bomb].bodyType = "kinematic"
bombs[bomb].isSensor = true
bombs[bomb].y = math.random(-100, -10)
bombGroup:insert(bombs[bomb])
bombs[bomb]:setLinearVelocity(0, math.random(100, 110))
function onCollision(e)
if e.phase == "began" then
bombs[bomb]:removeSelf()
bombs[bomb] = nil
end
end
bombs[bomb]:addEventListener("collision", onCollision)
end
tmrSpawnBomb = timer.performWithDelay(200, spawnBomb, 0)
local function cleanUp()
if bombGroup.numChildren then
for i=bombGroup.numChildren, 1, -1 do
if bombGroup[i].y \> 150 then
bombGroup[i]:removeEventListener("collision", onCollision)
bombGroup[i]:removeSelf()
end
end
end
end
tmrCheckBubble = timer.performWithDelay(20, cleanUp, 0)
Is there any problem spawning object this way? I spawn a few other objects this way too and print(collectgarbage(“count”)) show the number keep increasing. It reach 2500 (2.5MB?) in 10 minutes, is this increasing rate memory leak? Thanks.
Steve
[import]uid: 84159 topic_id: 14805 reply_id: 314805[/import]
(If you look at the sample code available you will notice event listeners aren’t manually removed when removeSelf() is used.)