lua-users home
lua-l archive

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


Am 03.05.2014 21:15 schröbte Karel Tuma:
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.

How does it leak memory? The old handle is collected after the `__gc` has run. See here[1] for the basic idea ...


Philipp


  [1]: http://lua-users.org/lists/lua-l/2013-12/msg00406.html