[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: __gc and setmetatable()
- From: Hartmut Henkel <hartmut_henkel@...>
- Date: Tue, 28 May 2013 13:23:49 +0200 (CEST)
Hi,
in the following code, with Lua 5.2.2...
a = {}
setmetatable(a, a)
a.__tostring = function() return "a tostring" end
a.__gc = function(t) print("a gc") end
print("table:", a)
a = nil
collectgarbage("collect")
print("=====")
b = {}
b.__tostring = function() return "b tostring" end
b.__gc = function(t) print("b gc") end
setmetatable(b, b)
print("table:", b)
b = nil
collectgarbage("collect")
print("=====")
...the gc is triggered only for table b. But the __tostring metamethod
works for both a and b. So somehow __gc and __tostring differ regarding
when the function setmetatable() is called. Why is this so?
Regards, Hartmut