Here is my test code :
mt={}
myt={}
setmetatable(myt, mt)
mt.__gc=function()print("hello gc")end
myt=nil
collectgarbage()
But, when i running this code, there's no message print.
So, I think there must something wrong in my code.
Update:
mt.__gc=function()print("hello gc")end
setmetatable(myt, mt)
Set __gc field before setmetatable would solve the question(using the online interpreter), but, when I executing the same code through C API luaL_dofile in my program, __gc is dead again.
And I also test in my standalone lua interprete(lua 5.1.5), and '__gcc' is dead too.
The only working fine case is that online interpreter which version is 5.3.
So, I should probably ask how can I get '__gc' to work under my 5.1 version ?
__gcmetamethod should be in metatable when you callsetmetatable. And ofcourse it works only since Lua 5.2 - moteussetmetatable({}, {__gc = function() print("hello gc") end}) collectgarbage()Tested on Lua 5.2 - moteus