Am I garbage collecting properly?

I have a table full of rectangles, like so:

[lua]local rectanglez = {}

–Simplified original function greatly; this code isnt tested
for i,4 do
rectanglez[i] = display.newRect(0,0,20,20)
rectanglez[i].x = i *30
rectanglez[i].y = i * 30
localGroup:insert(rectanglez[i])
end[/lua]
And lets say I want to eventually “nuke” every rectangle in that array:

[lua]–Actual function
function nukeTable(someGroup,someTable)
print(“Go nuke table”)
for i=#someTable,1,-1 do
local item = someTable[i]

if item.parent then
item.parent:remove(item)
– item.removeSelf()
item = nil
end
table.remove(someTable,i)
someTable[i] = nil
end
end

–At some point later in my code, I call the above using the following
nukeTable(localGroup,rectanglez)[/lua]

The above function seems to be working right now. My rectangles disappear from the screen and everything seems legit. I just want to make sure there’s no other additional steps I need to take to clear out the stuff from memory. Someone also suggested that I try the “removeSelf” function, but I have it commented out because I must not be using it right (it causes my application to crash). As always, any input is greatly appreciated :wink:

Cheers!
[import]uid: 52208 topic_id: 21836 reply_id: 321836[/import]

item.parent:remove(item)
– item.removeSelf()
item = nil

You are removing the item from the group (default group unless otherwise moved), and then removing it again. [import]uid: 79135 topic_id: 21836 reply_id: 86746[/import]

Are you saying --item.removeSelf() and item = nil is the same thing? That would make sense I guess… so I should be safe to leave the function as-is? [import]uid: 52208 topic_id: 21836 reply_id: 86888[/import]

removeSelf() removes the object from the screen and then sets the variable to nil.
Setting a variable to nil means it stays on the screen but you are then unable to access it since you have removed the connection.

I see now I phrased my previous answer badly. The reason that it was crashing is because you are trying to remove an object twice (the second instance removing a non existent object = crash)

You can use item.parent:remove(item) OR item.removeSelf()
but not both. [import]uid: 79135 topic_id: 21836 reply_id: 86891[/import]