(Yet Another :) removeSelf() on Physics body Question/Problem

Spent way too long on this-
Can anyone tell me why the following blows up?
The collision call-back removes the object successfully but if you try to remove the second object sometime later, it crashes the simulator. (see removeMine() and cleanUp() below)

Thanks!

require "sprite"  
  
display.setStatusBar( display.HiddenStatusBar )  
  
local physics = require "physics"  
physics.start()  
physics.setDrawMode( "hybrid" )  
  
local guy = display.newImage("armyguySmall.png")  
physics.addBody( guy, {radius=10.0, filter={ categoryBits = 2, maskBits = 5 } } )  
guy.x = 100; guy.y = 100  
guy.name = "bob"  
guy.pType = "enemy"  
  
local mine1 = display.newImage("claymore3.png")  
mine1.x = 100; mine1.y = 200  
physics.addBody( mine1, "static", { isSensor = true, density = 1.0, friction = 1.0, bounce = 0.0, radius=10.0, filter={ categoryBits = 4, maskBits = 2 } } )  
mine1.name = "mine1"  
mine1.pType = "mine"  
  
local mine2 = display.newImage("claymore3.png")  
mine2.x = 30; mine2.y = 250  
physics.addBody( mine2, "static", { isSensor = true, density = 1.0, friction = 1.0, bounce = 0.0, radius=10.0, filter={ categoryBits = 4, maskBits = 2 } } )  
mine2.name = "mine2"  
mine2.pType = "mine"  
  
-- THIS WORKS GREAT  
local function removeMine(obj)  
 print("removing mine 1")  
 mine1:removeSelf()  
end  
  
-- COLLISIONS ---------------------------------------------------------------------------------  
local function onCollision( event )  
 local obj1, obj2 = event.object1, event.object2  
 if ( event.phase == "began" ) then   
 print( "began: " .. obj1.name .. " & " .. obj2.name )  
  
 if (obj2.pType == "enemy") then  
 if (obj1.pType == "mine") then  
 obj2:removeSelf()  
 if (obj1.pType == "mine") then  
 timer.performWithDelay(1000, removeMine(obj1), 1)   
 end  
 end  
 end  
 end  
end  
Runtime:addEventListener("collision", onCollision)  
-- THIS BLOWS UP THE SIMULATOR  
function cleanUp()  
 print("remove mine 2 and explode!")  
 mine2:removeSelf()  
end  
  
timer.performWithDelay(3000, cleanUp, 1)  

[import]uid: 7587 topic_id: 2198 reply_id: 302198[/import]

Okay I probably missed this somewhere but it’s only when I set it to hybrid or debug mode that it blows up.
Normal mode appears to work fine.

This appears similar to another bug reported but doesn’t involve an off-screen object- http://developer.anscamobile.com/forum/2010/09/19/anyone-else-having-problem-physicssetdrawmode

And you really only need the following for it to blow:

require "sprite"  
  
display.setStatusBar( display.HiddenStatusBar )  
  
local physics = require "physics"  
physics.start()  
physics.setDrawMode( "hybrid" )  
local mine2 = display.newImage("claymore3.png")  
mine2.x = 30; mine2.y = 250  
physics.addBody( mine2, {} )  
function cleanUp()  
 print("remove mine 2 and explode!")  
 mine2:removeSelf()  
end  
  
timer.performWithDelay(500, cleanUp, 1)   

[import]uid: 7587 topic_id: 2198 reply_id: 6652[/import]

by the way in your initial code your second condition is pointless

[lua]if (obj1.pType == “mine”) then
obj2:removeSelf()
if (obj1.pType == “mine”) then – you’ve already checked this
timer.performWithDelay(1000, removeMine(obj1), 1)
end
end[/lua]

can be written as:

[lua]if (obj1.pType == “mine”) then
obj2:removeSelf()
timer.performWithDelay(1000, removeMine(obj1), 1)
end[/lua]

possibly though you’ve not allowed for a second collison

eg

mine1 collides with mine2 calling onCollision, mine1 is set to destroy in 1 second
but also that means:
mine2 collides with mine1, again calling onCollision, so mine2 is set to destroy in 1 second

but when you call cleanUp in 3 seconds, you’ve already removed mine2 in your second collision function, so when the timer tries to remove mine2 it wont be there anymore. causing an error

I think that’s what’ll happen anyway. You probably saw mine2 disappearing and thought it was the cleanUp routine but actually it was the second collision call’s timer

[import]uid: 6645 topic_id: 2198 reply_id: 10367[/import]