Memory leak when removing object with physics body in a collision..

I have a gameloop function which initiates enemy creation, and a collision event where I destroy the enemy object… but every time I remove the object inside a collision I got memory leak… instead of memory going down then up (cause after I remove, it also triggers the gameloop to produce another enemy when existing enemy is destroyed)… it goes up and up and up… :slight_smile: help needed guys…

[code]

–>>heres the collision event…

local function onCollision(self, event )  
  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
--\> 1st Enemy Object Colision with Ball  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 if self.name == "enemy" and event.other.name == "ball" and event.phase == "began" then  
  
  
 enemy:removeSelf()  
 enemy = nil  
  
 function removeEnemy()  
 enemy1IsPresent = 0  
 initGameLoop()  
 end  
  
  
 explodeAnim = display.newImageRect("poof-f1.png" , 100, 100 )  
  
 explodeAnim.x = self.x  
 explodeAnim.y = self.y  
  
  
  
 transition.to( poofAnim, { time=1000, alpha = .50,onComplete =function() poofAnim:removeSelf() poofAnim = nil removeEnemy() end} )  
 enemiesLayer:insert(poofAnim)  
  
--\>\>heres my gameloop code...  
  
function gameLoop()  
  
if gameIsActive then  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
--\> 1st Enemy Create()  
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
if enemy1IsPresent == 0 then  
  
 enemy = movieclip.newAnim({ "goblin1.png","goblin1.png","goblin1.png","goblin1.png","goblin1.png","goblin1.png","goblin1.png","goblin1.png","goblin1.png", "goblin1h.png","goblin1h.png","goblin1h.png","goblin1h.png","goblin1h.png","goblin1h.png", "goblin1h.png","goblin1h.png","goblin1h.png" }, 85, 85 )  
 physics.addBody(enemy, "dynamic", {radius = 40, filter = {categoryBits = 2, maskBits = 1}})  
 enemy.name = "enemy"  
 enemiesLayer:insert(enemy)  
 enemy.x = math.random(50, display.contentWidth - 50)  
 enemy.y = math.random(0,100)  
 enemy:play{ startFrame=1, endFrame=18, loop=-1,remove=true }  
 enemy.collision = onCollision  
 enemy:addEventListener("collision", enemy)  
 enemy1IsPresent = 1  
  
end  
 end  
 end  

I was wondering what causes the leak… Any help would be greatly appreciated. Thanks [import]uid: 76899 topic_id: 14742 reply_id: 314742[/import]

It actually looks like you’re trying to remove your enemy before the collision is resolved, which would cause an error. [import]uid: 52491 topic_id: 14742 reply_id: 54525[/import]

Indeed. You can’t remove a body until the collision is resolved.

You will need to do so with a slight delay.

Ie :

[code]

local function removeWithDelay(event)
obj:removeSelf()
obj = nil

return 0
end

timer.performWithDelay(2, removeWithDelay)

[/code] [import]uid: 84637 topic_id: 14742 reply_id: 54543[/import]

Thanks for the reply… but delaying to remove the enemy doesn’t resolve the problem… Yes it does free up some memory when I destroy the object but when the next one spawn, the memory doubles up… like:

before collision:
MemUsage: 610.86328125
TexMem: 6.946816

after collision (obj is destroyed)

MemUsage: 608.86328125
TexMem: 6.946816

then when the next spawn occurs:

MemUsage: 614.86328125
TexMem: 6.946816
Thanks Peach and Danny
:slight_smile: [import]uid: 76899 topic_id: 14742 reply_id: 54633[/import]

It looks like it’s the way your spawning objects then

Is that exactly how you are spawning them (above) ? [import]uid: 84637 topic_id: 14742 reply_id: 54691[/import]