lua-users home
lua-l archive

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


Is it okay to set the metatable of a userdata to nil?  In my C++
program, a Lua function handles a C++ event.  I'm pasting to it a
userdata that has a metatable associated with it.  I want to prevent the
functions of the userdata's metatable from being called after the event
has occurred.  I'm thinking I can do this by setting the userdata's
metatable to nil after the event has been invoked.

   Nriver **pd = (Driver **)lua_newuserdata(_L, sizeof(Driver *));
   *pd = driver;

   lua_rawgeti(_L, LUA_REGISTRYINDEX, _HandleEvent);

   lua_pushvalue(_L, -2);
   luaL_getmetatable(_L, "Driver*");
   lua_setmetatable(_L, -2);

   int t = lua_gettop(_L);
   lua_pcall (_L, 1, 0, 0);
   lua_settop(_L, t);

   lua_pushnil(_L);
   lua_setmetatable(_L, -2);


function HandleEvent(driver)
	-- the following is dangerous
	mydriver = driver
end