Making FFI callbacks call free() automatically when it's collected

  • From: Johnson Lin <arch.jslin@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 19 Jul 2012 16:33:51 +0800

Hello list,

Since anonymous FFI callbacks are anchored permanently, and explicitly
call cb:free() every time is pretty troublesome too, is it considered
good to do this?

local cb = ffi.gc(ffi.cast("CALLBACK", function() end), function(self)
self:free() end)

cb()
cb = nil
collectgarbage("collect")

I did print some messages in the callback body and the finalizer body
to verify it's working.

Or is there a better way?

best regards,
Johnson

Other related posts: