My mistake, my apologies. What the code should have been > M = function() > local myObj = createRObject() > myObj = functionToProtect(myObj) > mObj = ffi.gc( myObject, functionToUnProtect) > -- do something > return nil But your answer to (2) is what i wanted to know. Thanks Saptarshi On Thu, Mar 6, 2014 at 9:42 AM, Mike Pall <mike-1403@xxxxxxxxxx> wrote: > Saptarshi Guha wrote: > > M = function() > > myObj = createRObject() > > myObj = functionToProtect(myObj) > > mObj = ffi.gc( myObject, functionToUnProtect) > > -- do something > > return nil > > You're writing to global variables. That's not a good idea in > general. > > > 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. > > Sure, the global keeps the object alive. > > > If i repeat steps (1) and (2) again, then it does get collected. > > Because the global now holds the new object and the old object is > no longer anchored. > > > 1. Doesn't lua_gc call the GC? > > It does. But that's an expensive thing to do. Not something you'd > want to do after every call. > > > 2. If so, do i have expectations on the time bounds or ordering of > > collections? > > Nope. > > --Mike > >