[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Losing metatable reference
- From: Sean Conner <sean@...>
- Date: Thu, 21 Jul 2011 17:27:10 -0400
It was thus said that the Great Murray S. Kucherawy once stated:
>
> However, lua_getmetatable() returns 0, apparently unable to find the
> associated metatable. I've confirmed that the pointer created by func()
> is the same as the one being passed to use().
>
> Any suggestions? I'm really puzzled; it shouldn't be garbage collection
> because the script hasn't terminated and the values haven't been
> reassigned on the script side, so that pointer should still reference
> something valid (and I checked that too; it does).
Did you register the metatable in the registry?
luaL_newmetatable(L,name);
Looking into the source of Lua, luaL_getmetatable(L,n) is a macro for
lua_getfield(L,LUA_REGISTRYINDEX,n), which will return a nil value for a
non-existent field. nil is a Lua value, which I assume is not to be
confused with C's NULL, so what you might be seeing is an actual nil value
on the stack. Try:
ud = lua_touserdata(L, n);
if (ud != NULL)
if (lua_getmetatable(L, n) != 0) {
lua_getfield(L, LUA_REGISTRYINDEX, name);
assert(!lua_isnil(L,-1));
if (lua_rawequal(L, -1, -2)) {
lua_pop(L, 2);
return ud;
}
lua_pop(L, 2);
}
}
return NULL;
and see if the assert() fires.
-spc (That's be my guess ... )