lua-users home
lua-l archive

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


Answering myself:
Will a userdata's address change when the gc runs ?

ie:

lua_pushstring(L,"foo");
p = lua_newuserdata(L,sizeof(foo));
lua_rawset(L,LUA_REGISTRYINDEX);
-- note the userdata is no longer on the stack here

... do something to force a gc

lua_pushstring(L,"foo");
lua_rawget(L,LUA_REGISTRYINDEX);
p1 = lua_touserdata(L,-1);
assert (p==p1); <----------------

Adrian


I would guess that the answer is that a (full) usedata would be safe as long as it is on the stack, but that the gc may move the ud (and thus invalidate the pointer), once it is off the c stack.

This would imply that having full userdata on the stack may cause the garbage collector to be unable to run to completion.

Adrian