What is the proper way to remove objects? I'm receiving an error

Sometimes throughout my game, I get an error message in the terminal which is about removing objects. The error message only comes up like once or twice throughout the game but sometimes the object freezes without being removed. This is the only thing that is holding me back and I can’t find out what I’m doing wrong. Here is the error message:

"Runtime error
ERROR: Attempt to remove an object that’s already been removed from the stage or whose parent/ancestor group has already been removed.
stack traceback:

C: in function ‘removeSelf’
?: in function ‘remove’

This error message only comes up whenever I hit a bomb with a bullet. I want the bomb to be completely removed whenever it’s hit with a bullet so I can show an animation there. So whenever a bullet hits a bomb, the bomb is removed like this:

[lua]if object1.name == “bomb” and object2.name == “bullet” then
local function turnOffBody()
object1.isBodyActive = false

–Destroy the bullet and bomb when it collides with the bomb
display.remove(object1)
display.remove(object2)
end
timer.performWithDelay(1, turnOffBody)
end
end
return true
end [import]uid: 69494 topic_id: 31588 reply_id: 331588[/import]

when removing a display object, the object stays in memory as a table.
so you need to set the object to nil.
and when you do this, you can check if an object is nil before attempting to remove it.

if object1 ~= nil and object2 ~= nil then  
 display.remove(object1)  
 display.remove(object2)   
 object1 = nil  
 object2 = nil  
end  

hope that helps :wink:
-finefin [import]uid: 70635 topic_id: 31588 reply_id: 126215[/import]

It sort of worked. My objects are not freezing at random times and they’re disappearing but the error message still appears for some reason :frowning:

Perhaps maybe I need to remove the parent object? I’m not quite sure how to do that.

btw thanks for your help! That is good information to keep in mind. [import]uid: 69494 topic_id: 31588 reply_id: 126247[/import]

So it seems I have to remove the parent or ancestor group of the objects. Does that make any sense to anyone? I thought I am removing the objects the correct way. I set the objects to nil but now the error message comes up once everytime I play the level. [import]uid: 69494 topic_id: 31588 reply_id: 126324[/import]

hm… I think I know what’s going on.
if you do something like in line 1 of your snippet, you need to check if the object is not nil *before* you check any attributes/vars/tables of that object, or an error will fire

if object1 ~= nil and object2 ~= nil then if object1.name == "bomb" and object2.name == "bullet" then -- do stuff end end [import]uid: 70635 topic_id: 31588 reply_id: 126351[/import]

Unfortunately it’s not working :frowning:

The objects act strange when I try that method. The bombs already explode and fall at the same time, they don’t get removed when they’re hit by a bullet. [import]uid: 69494 topic_id: 31588 reply_id: 126366[/import]

when removing a display object, the object stays in memory as a table.
so you need to set the object to nil.
and when you do this, you can check if an object is nil before attempting to remove it.

if object1 ~= nil and object2 ~= nil then  
 display.remove(object1)  
 display.remove(object2)   
 object1 = nil  
 object2 = nil  
end  

hope that helps :wink:
-finefin [import]uid: 70635 topic_id: 31588 reply_id: 126215[/import]

It sort of worked. My objects are not freezing at random times and they’re disappearing but the error message still appears for some reason :frowning:

Perhaps maybe I need to remove the parent object? I’m not quite sure how to do that.

btw thanks for your help! That is good information to keep in mind. [import]uid: 69494 topic_id: 31588 reply_id: 126247[/import]

I know I sound really desperate and I feel really bad for saying this but this is the only thing that is holding my project back and I need some help with this as soon as possible lol. I tried to follow a number of tutorials on how to properly remove objects and set them to nil but nothing seems to work for some reason. I do feel like the code is done correctly but removing them is just the only problem I’m having. So if someone has a suggestion, please share :slight_smile: [import]uid: 69494 topic_id: 31588 reply_id: 126472[/import]

Not sure if you still need help with this, but a good way to deal with object removal is to just pool them. Basically, store a number of bombs and bullets in separate arrays & set the isVisible property to false. Then remove one from the array when you need it, and add it to the whichever display group. You don’t need to worry about completely removing the object until you unload your scene, and Corona’s scene manager does this for you anyways.

Pooling is explained my post here: http://www.ardentkid.com/blog/2012-10-03/layering-display-and-asset-pooling [import]uid: 36792 topic_id: 31588 reply_id: 126520[/import]

So it seems I have to remove the parent or ancestor group of the objects. Does that make any sense to anyone? I thought I am removing the objects the correct way. I set the objects to nil but now the error message comes up once everytime I play the level. [import]uid: 69494 topic_id: 31588 reply_id: 126324[/import]

hm… I think I know what’s going on.
if you do something like in line 1 of your snippet, you need to check if the object is not nil *before* you check any attributes/vars/tables of that object, or an error will fire

if object1 ~= nil and object2 ~= nil then if object1.name == "bomb" and object2.name == "bullet" then -- do stuff end end [import]uid: 70635 topic_id: 31588 reply_id: 126351[/import]

Unfortunately it’s not working :frowning:

The objects act strange when I try that method. The bombs already explode and fall at the same time, they don’t get removed when they’re hit by a bullet. [import]uid: 69494 topic_id: 31588 reply_id: 126366[/import]

I know I sound really desperate and I feel really bad for saying this but this is the only thing that is holding my project back and I need some help with this as soon as possible lol. I tried to follow a number of tutorials on how to properly remove objects and set them to nil but nothing seems to work for some reason. I do feel like the code is done correctly but removing them is just the only problem I’m having. So if someone has a suggestion, please share :slight_smile: [import]uid: 69494 topic_id: 31588 reply_id: 126472[/import]

Not sure if you still need help with this, but a good way to deal with object removal is to just pool them. Basically, store a number of bombs and bullets in separate arrays & set the isVisible property to false. Then remove one from the array when you need it, and add it to the whichever display group. You don’t need to worry about completely removing the object until you unload your scene, and Corona’s scene manager does this for you anyways.

Pooling is explained my post here: http://www.ardentkid.com/blog/2012-10-03/layering-display-and-asset-pooling [import]uid: 36792 topic_id: 31588 reply_id: 126520[/import]

Hey ArdentKid, thanks for the suggestion but unfortunately that is not what I am looking for. It seems too complicated.

I did try this but I am still receiving the same error message. I don’t seem to understand why this is happening. The funny thing is, this only happens when one of my bullets hits 2 bombs at the same time, then the error message appears. [import]uid: 69494 topic_id: 31588 reply_id: 126747[/import]

Hmm, the only possibility I can think of without seeing the rest of your code is to make sure you’re not removing any bullets/ bombs from a table while looping through it. Sorry, let us know if you find a solution!

[import]uid: 36792 topic_id: 31588 reply_id: 126748[/import]

@ArdentKid thank you for the pooling method! it works like a charm and really makes a difference! [import]uid: 70635 topic_id: 31588 reply_id: 126778[/import]

Have you tried checking
if obj then display.remove(obj) obj=nil

in each step? If you put up some plug and play happy to test this for you. [import]uid: 52491 topic_id: 31588 reply_id: 126915[/import]

Hey ArdentKid, thanks for the suggestion but unfortunately that is not what I am looking for. It seems too complicated.

I did try this but I am still receiving the same error message. I don’t seem to understand why this is happening. The funny thing is, this only happens when one of my bullets hits 2 bombs at the same time, then the error message appears. [import]uid: 69494 topic_id: 31588 reply_id: 126747[/import]