lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Excerpts from Philipp Janda's message of 2014-05-03 20:50:26 +0200:
> I would split your object into two parts: One frontend handle, and one 
> backend stored via `luaL_ref` in the registry. The frontend handle (Lua 
> table in 5.2, proxy userdata in 5.1) forwards all calls the the real 
> thing and has a __gc that checks for reachability on "the other side". 
> If you are still reachable over there, you create a _new_ frontend 
> handle and put it into your weak table for the next garbage collection 
> run to check again. In case you aren't reachable on the other side 
> either, you `luaL_unref` the backend userdata, which will be collected 
> eventually.
This is the workaround I originally tried. Unfortunately it leaks
memory as you need to create and link chain new dummy object to get
another __gc notification after each resurrect incident.

Now, with language bridges it is quite common to pass object back and
forth between VMs (while not keeping reference in source VM) -
and gc happening in between. So dummy finalizer object eats memory away
while no any of it is actually necessary and all of this is torn down
after the genesis object is finally gone (if ever).

If you're familiar with V8, the relevant code is:
https://github.com/katlogic/lv8/blob/master/lv8.cpp

(There are some UB VM tricks for both Lua and V8 to cut down
some of the abstraction overhead).