Hi all,
I am just wondering what is the best practice for removing objects, I want to make sure that I have not unintentionally made memory leak. My question is best illustrated in an example.
If I have a module I use to create characters in a game and I add a bunch of methods to this object.
[lua]
local public = {}
public.new = function()
local g = display.newGroup()
local character = display.newCircle(g, 100,100,100)
function g:destroy () – called to destroy character, could be called as part of collision or custom event
display.remove(self)
end
function g:finalize ()
– clean up timers and runtimes
print(“removing g”)
self = nil – is this needed?
end
g:addEventListener(“finalize”)
return g
end
return public
[/lua]
And I run the following code in my main.lua
[lua]
local t = require (“test”)
local obj = t.new()
print(obj) – prints “table: 0x7fee98f89780”
obj:destroy()
print(obj) – prints “table: 0x7fee98f89780”
[/lua]
Is this obj cleaned up properly? Since obj still contains a reference to the table does this create a memory leak, or will would this be handled by scope if I changed scenes?
Do I need to “self = nil” in the finalize event for the object?
Does it work the same in a table?
[lua]
local objects = {}
for i = 1, 5 do
objects[i] = t.new()
end
objects[3]:destroy()
for i = 1, 5 do
print(objects[i]) – prints table reference
end
[/lua]
Thanks in advance,
Craig