Re: Segfault from nil table access when using ffi.gc

  • From: Peter Cawley <corsix@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 13 Nov 2016 16:34:23 +0000

On Sun, Nov 13, 2016 at 3:07 PM, Gerhard Fuernkranz <nospam456@xxxxxx> wrote:

Is a live reference to a function in a C namespace not supposed to keep the
namespace object live as well, preventing unload of the .so?

That would certainly be nice, but it is a bit fiddly to make work.
First, note that a C namespace maintains a cache of name to
function-like cdata, in the form of a table. To get the friendly
semantics, we'd need a weakly-keyed table somewhere mapping from
function-like cdata back to C namespace. Said weakly-keyed table would
either need to be an ephemeron table (newgc), or the namespace cache
tables would need to be made weakly-valued. At this point, the
function-like cdata would keep their associated namespaces alive, but
there is a remaining problem: if the namespace and the function-like
cdata and a value-like cdata which has the function-like cdata as its
finaliser all become collectable in the same GC cycle, then the
finalizer for the namespace and the finalizer for the value-like cdata
need to be called in the correct order (this is topologically-correct
finalizer order, which - like ephemeron tables - is a newgc feature).

Other related posts: