Crash when removing objects on collision

local enemy  
local enemyNumber  
  
local function choose1()  
  
 local choose = rnd(1, 2)  
  
 -- If satsen  
 if(choose == 1) then  
 if(ballSizeEnemy \<= 8) then  
 enemyNumber = 1  
 end  
  
 enemy = display.newImage(""..enemyNumber..".png", rnd(0, 480), -140)  
 localGroup:insert(enemy)  
 worldGroup:insert(enemy)  
  
 -- Radie på enemy  
 if(enemyNumber == 1) then  
 enemySize = 4  
 end  
  
  
  
 -- Fysik  
 physics.addBody(enemy, { density=1.0, friction=0.3, bounce=0,   
 radius=enemySize, isSensor=true })  
  
 if(enemyNumber == 1) then  
 enemy:applyForce(0, rnd(3, 7), 0, 0)  
 end  
  
 -- Opacy  
 enemy.alpha = 0.7  
  
 -- Lägg in i grupp  
 enemyGroup:insert(enemy)  
  
 -- Ingen rotation  
 enemy.isFixedRotation = true  
  
 -- Is Collideable  
 enemy.isCollideable = true  
  
 local function enemyCollision(self, event)  
  
 if(event.phase == "began" and event.other.class == "enemyCleaner") then  
  
 self:removeSelf()  
 self = nil  
  
 elseif(event.phase == "began" and event.other.class == "eastWall") then  
  
 self:removeSelf()  
 self = nil  
  
 end  
  
  
 end  
 enemy.collision = enemyCollision  
 enemy:addEventListener("collision", enemy)   
  
  
  
 -- Slut på If satsen  
 end  
 return true  
  
end  
local enemyTimer = performAfterDelay(0.3, choose1, 0, true)  

Ok, so I’ve got this code above. Everything works great in the latest official build (268), but now when I tried this same code in the latest build (312) the game crashes. It crashes every time enemy collides with either enemyCleaner or eastWall. I remember someone long ago telling me that removing physical objects upon collision would cause crashes, but the code works great in build 268?

Thanks in advance, [import]uid: 30185 topic_id: 7727 reply_id: 307727[/import]

Exactly the same issue I have been having and asked this morning.

http://developer.anscamobile.com/forum/2011/03/11/remove-object-causes-crash-simulator

Mike R [import]uid: 9950 topic_id: 7727 reply_id: 27400[/import]

Make sure the item isn’t in transition when removing. This is especially important post collision, you can’t access a number of properties if this is true. If it is, you have to make sure to cancel the tween first.

[lua]moveobject = transition.to(item, {
time=680, alpha=1,
x=centerx - 120, y=centery,
transition=easing.outExpo
})[/lua]

[lua]transition.cancel(moveobject)[/lua]

Now you can set the object properties:
[lua]item.isBodyActive = false
item:setLinearVelocity(0, 0)
item.angularVelocity = 0[/lua]

And then you can remove the object.
[import]uid: 11024 topic_id: 7727 reply_id: 27407[/import]

Oh, do we have transition.cancel/pause/resume etc now?:slight_smile:

Well, the object is not in the middle of a transition or anything, it’s just created with a force pulling it down, once it reaches y: 500 it collides with enemyCleaner and is supposed to go away…

I tried adding the properties you suggested before removing the objects upon collision:

self.isBodyActive = false  
self:setLinearVelocity(0, 0)  
self.angularVelocity = 0  

But still I get the crash [import]uid: 30185 topic_id: 7727 reply_id: 27409[/import]

What if you change it to precollision? [import]uid: 11024 topic_id: 7727 reply_id: 27412[/import]

With precollision they wont collide, [import]uid: 30185 topic_id: 7727 reply_id: 27414[/import]

Any news on this? [import]uid: 14018 topic_id: 7727 reply_id: 27520[/import]

Hi,
Your fix works for me but it is very weird as I get error that I try to call method removeSelf a nil value, but if it isn’t there it crashes…
Hope that get fixed soon.

Cheers [import]uid: 27699 topic_id: 7727 reply_id: 27596[/import]

Found the problem! It seems that the object you’re trying to remove is in a group.

Try do myGroup:remove(myObject) before removing the object. This solved it for me anyway:-) [import]uid: 14018 topic_id: 7727 reply_id: 27523[/import]

it has been. pick up the latest build. [import]uid: 24 topic_id: 7727 reply_id: 28291[/import]

greatttt! [import]uid: 14018 topic_id: 7727 reply_id: 28388[/import]