[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Caveat with lua_pushlightuserdata(L, (void *)&key)
- From: Mike Pall <mikelu-0606@...>
- Date: Sat, 24 Jun 2006 13:51:28 +0200
the standard practice of using the address of a static const
object as a unique lightuserdata key is problematic:
static const int regkey_foo = 1;
static const int regkey_bar = 1;
lua_pushlightuserdata(L, (void *)®key_foo);
lua_pushlightuserdata(L, (void *)®key_bar);
PIL 27.3 describes this technique as "bulletproof" to get a
unique address. Alas, it's not.
The problem is that the compiler/linker may decide to join
constants with identical _values_. Unfortunately this makes the
_addresses_ the same, too. This in turn means the lightuserdata
keys are the same and happily overwrite each other in the
Note that the linker may even decide to join constants from
different source files, linked to the same executable or shared
[This is not a theory: LuaJIT 1.1.1 was broken on Mac OS X due to
this issue (GCC 4.0 + Mach linker).]
- Ensure the const _values_ are unique across your whole program.
- Or use the addresses of non-const static variables.
Better check your programs now -- this is very hard to diagnose.