lua-users home
lua-l archive

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


Alex Bradbury wrote:
> I can imagine binding C APIs by creating Lua objects that hold a
> reference to the C object. If the library lets you allocate the memory
> yourself, then garbage collection can be handled for you. Otherwise
> just the pointer cdata will be gced, so you must write your own __gc
> metamethod to call the library's free function - but this isn't
> supported on tables. Is there a better way than creating a newproxy
> for each object and giving it a __gc metamethod that makes the
> appropriate free call?

You raise a valid point. Currently wrapping it in userdata is the
only way to trigger the __gc metamethod. Although cdata objects
will gain user-definable metamethods (per type, not per instance),
the __gc metamethod won't work on them with the current GC. You'll
have to wait for LuaJIT 2.1.

--Mike