Tomash Brechko
2014-09-18 15:17:40 UTC
Hello!
Consider the following code:
ffi = require("ffi")
ffi.cdef[[ struct S {}; struct S *malloc(size_t size); ]]
ffi.metatype("struct S", {
__len = function() return 5 end,
__gc = function() print("GC") end
})
s1 = ffi.new("struct S")
s2 = ffi.C.malloc(1)
print(#s1) -- Outputs "5" - good
print(#s2) -- Outputs "5" - good
Now we do the following:
collectgarbage()
s1 = nil
collectgarbage() -- Outputs "GC" - good
s2 = nil
collectgarbage() -- Output nothing - why?
Not claming that it's a bug, my guess this is intentional and one has to do
ffi.gc(s2, ...), but why? What is the rationale for not calling metatype
__gc when object is allocated outside of LuaJIT?
Consider the following code:
ffi = require("ffi")
ffi.cdef[[ struct S {}; struct S *malloc(size_t size); ]]
ffi.metatype("struct S", {
__len = function() return 5 end,
__gc = function() print("GC") end
})
s1 = ffi.new("struct S")
s2 = ffi.C.malloc(1)
print(#s1) -- Outputs "5" - good
print(#s2) -- Outputs "5" - good
Now we do the following:
collectgarbage()
s1 = nil
collectgarbage() -- Outputs "GC" - good
s2 = nil
collectgarbage() -- Output nothing - why?
Not claming that it's a bug, my guess this is intentional and one has to do
ffi.gc(s2, ...), but why? What is the rationale for not calling metatype
__gc when object is allocated outside of LuaJIT?
--
Tomash Brechko
Tomash Brechko