Object - Garbage Collection Question

Hi all,

Imagine that we have an object:

local obj = {}  
obj.id = 0  
obj.increase = function()  
 obj.id = obj.id + 1  
end  
obj.print = function()  
 print(obj.id)  
end  

I added a destroy function:

local obj = {}  
obj.id = 0  
obj.increase = function()  
 obj.id = obj.id + 1  
end  
obj.print = function()  
 print(obj.id)  
end  
obj.destroy = function()  
 obj.id = nil  
 obj.increase = nil  
 obj.print = nil  
 obj.destroy = nil  
end  

Do I need a destroy function? When I set to obj = nil does GC remove all variables and methods in obj? [import]uid: 5629 topic_id: 24207 reply_id: 324207[/import]

If your setting a table to nil, then it’s sub-properties/variables will also be nill’ed [import]uid: 84637 topic_id: 24207 reply_id: 97732[/import]

Thanks for the fast reply.

Let’s consider this scenario:

local obj = display.newGroup()  
obj.id = 0  
obj.rect0 = display.newRect(0,0,10,10)  
obj.rect1 = display.newRect(0,0,10,10)  
obj.rect2 = display.newRect(0,0,10,10)  
  
obj:insert(obj.rect0)  
obj:insert(obj.rect1)  
obj:insert(obj.rect2)  
  
obj.id = 0  
obj.increase = function()  
 obj.id = obj.id + 1  
end  
obj.print = function()  
 print(obj.id)  
end  

I want to destroy obj:

display.remove(obj)  
obj = nil  

So these two lines remove obj group and its sub display objects. Then setting all the sub objects and methods to nill right? [import]uid: 5629 topic_id: 24207 reply_id: 97733[/import]