Properly removing display objects? Problem referring to object as "self" vs. actual object name?

I have a question about removing display objects…

I was under the impression a person should remove the object from the display and then specifically set it equal to nil so that it will get cleaned up.

Why does the first version leave an empty table I can reference? (display.remove(self) seems to remove all table information but self = nil doesn’t seem to remove the table it’s self)

[lua]

–this leaves the dwarfSprite table that I can reference but find no table values in (breaks my set up when I later check to see that dwarfSprite ~= nil but then it has no data in it:

function dwarfSprite:destroy()

        display.remove(self) --these weren’t working but if I specify the object then it does… what?!

        self = nil

end

–this actually removes the dwarfSprite table so that I am unable to reference it at a later time (desired - fails my dwarfSprite ~= nil check)

function dwarfSprite:destroy()

        display.remove(dwarfSprite)

        dwarfSprite = nil

end

[/lua]

You should remove display objects using either obj:removeSelf() or display.remove(obj) so that the display hierarchy is told to stop rendering the object.

Setting the object to nil is purely for your code. If you keep a reference to the object in memory it will not get properly cleaned up and, yes, you’ll be able to refer to the object even though it is no longer on the screen.

In your code above, actually setting self to nil is not necessary. What you need to do is set any references to the object you’ve created to nil. Setting an object’s references to itself to nil is not required because the garbage collector will see that those references are circular (lead back to themselves) and ignore them. (Garbage Collectors really are cool!)

So, to explain:

If you have a variable in your scene defined as:

local ball = display.newCircle(...) 

If you call “ball:removeSelf()” you would also need to call “ball = nil” to completely remove all references to it.

However, if you have a function attached the ball object which is, perhaps, listens for certain system events, there is no global variable to set to nil, so the function call call removeSelf() and be done with it:

function ball:tap(e)   ball:removeSelf()   -- no need to set 'ball' to nil because we have not set 'ball' to any value end

Let’s say it this way. Display object from programmer point of view (just simplifying) are like tables extended with special properties, because you can use properties of it (like x, y, scale and so one). However, one must remember that some parts of this table are not accessable from lua because they are used under the hood in Corona.
Display.remove simply jusy strips down this complex object looking as table to ordinary lua table, so it still occupies memory. That is why you must nil it as any table in lua you want to free.
Because of how lua works, assigning nil isn’t equal to removing. It simply means ‘this variable no longer points to the memory occupied by this table’. Lua will remove table only then when there is no variable pointing to it.

So check if there isn’t some variabls pointing to table. Display.remove will only remove visual part, but some references can still I e present.

You should remove display objects using either obj:removeSelf() or display.remove(obj) so that the display hierarchy is told to stop rendering the object.

Setting the object to nil is purely for your code. If you keep a reference to the object in memory it will not get properly cleaned up and, yes, you’ll be able to refer to the object even though it is no longer on the screen.

In your code above, actually setting self to nil is not necessary. What you need to do is set any references to the object you’ve created to nil. Setting an object’s references to itself to nil is not required because the garbage collector will see that those references are circular (lead back to themselves) and ignore them. (Garbage Collectors really are cool!)

So, to explain:

If you have a variable in your scene defined as:

local ball = display.newCircle(...) 

If you call “ball:removeSelf()” you would also need to call “ball = nil” to completely remove all references to it.

However, if you have a function attached the ball object which is, perhaps, listens for certain system events, there is no global variable to set to nil, so the function call call removeSelf() and be done with it:

function ball:tap(e)   ball:removeSelf()   -- no need to set 'ball' to nil because we have not set 'ball' to any value end

Let’s say it this way. Display object from programmer point of view (just simplifying) are like tables extended with special properties, because you can use properties of it (like x, y, scale and so one). However, one must remember that some parts of this table are not accessable from lua because they are used under the hood in Corona.
Display.remove simply jusy strips down this complex object looking as table to ordinary lua table, so it still occupies memory. That is why you must nil it as any table in lua you want to free.
Because of how lua works, assigning nil isn’t equal to removing. It simply means ‘this variable no longer points to the memory occupied by this table’. Lua will remove table only then when there is no variable pointing to it.

So check if there isn’t some variabls pointing to table. Display.remove will only remove visual part, but some references can still I e present.