lua-users home
lua-l archive

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

On 14.07.2014 22:51, Steven Degutis wrote:
I've got some C functions that take callback which has a void* pointer
for programmer-use. The technique I've been using is making a little
struct that holds the lua_State* and an int, which I store the return
value of luaL_ref'ing the given function. Then inside the C callback,
I take the Lua state and lua_rawgeti the function out of

But this seems very cumbersome and awkward. (Plus it means my function
won't be garbage-collected until I luaL_unref it out of there, but I'm
not sure that can be solved without making use of an explicit
"unregister" method for this C callback). Is there a better way to do
this that is less cumbersome and awkward? Thanks.

Well, instead of making a ref you can use the address of that little struct as a lightweight userdata when you access the registry. This won't solve the problem though, just make the struct smaller. In many cases you don't need the little struct. When it's in the code of executable, there normally is only one global Lua state, thus only the ref needs storing. You'll need to unregister the callback in any case. Only you know when you no longer need the function. Either when the object for which the callback is set gets destroyed or when you remove the callback or assign a different one.

Best regards,
Sergey Rozhenko