lua-users home
lua-l archive

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


I have some problems with adding a metatable to userdata for garbage collection. This is what I have now... When i start lua I do the following.

luaL_newmetatable(L, "LuaBook.gc_event");

/* set its __gc field */
lua_pushstring(L, "__gc");
lua_pushcfunction(L, gc_function);
lua_settable(L, -3);

I then call a factory function from lua which creates an object in C. I want to be able to free this object the right way when it is not used anymore, but it does not seem to work. This is what I do in the factory function.

luaL_getmetatable(L, "LuaBook.gc_event");
lua_setmetatable(L, -2);'

In the Lua program, I then do something like this.

o = CreateObject()
o = nil

When I run this the function I added to the metatable does not get called. What am I doing wrong here?


- Lyte

Dags att flytta till större?