lua-users home
lua-l archive

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


Am 13.03.2014 13:43 schröbte David Crayford:
Thanks,

It's my mistake for sure. I copied the code from lua-cjson and the
finalizer gets called in that code in 5.1
https://github.com/mpx/lua-cjson/blob/master/lua_cjson.c.

I know it's something stupid but I don't know the C API well enough to
fix it. The full code is here http://hastebin.com/likikexinu.coffee.

Your problem is that your finalizer method is pushed via `lua_pushcfunction` which doesn't include any upvalues, so you can't use your `fetch_config` function to access the userdata in `destroy_config`. The userdata is passed as first argument to the finalizer, so use

    ( ispf_config_t * )lua_touserdata( l, 1 );

instead. I'd recommend to prohibit access to your metatable, or use luaL_newmetatable and luaL_checkudata for additional type safety in your finalizer. There is also an `int` missing for `destroy_config`. The rest looks ok on first glance ...


On 12/03/2014 8:27 PM, Ignacio Burgueño wrote:
Unless I'm mistaken, that won't work with Lua 5.1 since the __gc
metamethod for tables was introduced in 5.2.
In this case, it does nothing. You'll need a userdata to achieve the
same effect.

The code (the one in the OP) already uses `__gc` on a userdata.


You should take a look at Thijs Schreijer's "Lua Library Template"

https://github.com/Tieske/Lua_library_template

For instance, this example does what you want:
https://github.com/Tieske/Lua_library_template/blob/master/udtype_example/udtype.c#L196



Regards,
Ignacio


Philipp