ffi: callback lifetime

  • From: Cosmin Apreutesei <cosmin.apreutesei@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 18 May 2012 15:33:49 +0300

Hi, there's something I'm not sure of regarding callback lifetime.

AFAIU implicit callback objects created by assigning a Lua function to
a function pointer are internally anchored along with the Lua function
and there's no way to access them or free them (or the Lua function).
But callbacks created by ffi.cast() are not anchored and thus subject
to normal garbage collection rules, and thus no need to "unanchor"
them by cb:free(), is that right?

So if I pass some_lua_function where a function pointer is expected,
the callback is permanent, but if I pass ffi.cast('WNDPROC',
some_lua_function) the callback is only guaranteed to last until the
function returns, and will be freed on the next gc cycle without me
having to cb:free() it, right? Or do I always have to manually
cb:free() it afterwards if I don't need it ? In other words, does a cb
object have a __gc that does self:free() ?

Other related posts: