Weak table and GC finalizer using C API
I am attempting to create a GC finalizer for a function value by storing it in a weak table using the C API. I started off by writing a prototype in pure Lua 5.2: local function myfinalizer() print 'Called finalizer' end function myfunc() print 'Called myfunc' end local sentinels = setmetatable({}, { __mode='k' }) sentinels[myfunc] = setmetatable({}, { __gc=myfinalizer }) myfunc() myfunc = nil collectgarbage 'collect' print 'Closing Lua' Resulting output: Called myfunc Called finalizer Closing Lua The prototype seems to be working as intended. Below is the C version: #include <stdlib.h>