Expectations on when GC is called

  • From: Saptarshi Guha <saptarshi.guha@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 6 Mar 2014 09:14:50 -0800

This is a question about when GC is triggered. I'm calling Lua from another
language. The flow (hopefully this is clear) is this

1. R calls C through the .Call interface
2. The C code calls my lua code (snippet)
    status = lua_pcall(L, narg, 1, base);
   What this Lua code might do is create an object in the R language. In
this
   language, one needs to 'protect' the variable from R' GC engine and then
when
   returning back to R, 'unprotect' this.  What I did was (for example)

   M = function()
     myObj  = createRObject()
     myObj = functionToProtect(myObj)
     mObj = ffi.gc( myObject, functionToUnProtect)
     -- do something
   return nil

   So when the lua code returns to C(after the pcall), i call

   lua_gc(L, LUA_GCCOLLECT, 0);

   to trigger the GC. I expect the lua GC to run and myObj to be collected
and
   functionToUnProtect to be run.

   However nothing is printed.

 If i repeat steps (1) and (2) again, then it does get collected.

 So, my question

 1. Doesn't lua_gc call the GC?
2. If so, do i have expectations on the time bounds or ordering of
collections?


I hope i've been clear, my apologies if not

Other related posts: