lua-users home
lua-l archive

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


I have this block of code in an app:

		 lua_rawgetp(pVM, LUA_REGISTRYINDEX, (void*) core_RCBAliasCreateLS);	// [-0,+1]=1 Get WAT to TOS 
		LuaAlias *pAlias = (LuaAlias*) lua_newuserdata(pVM, sizeof(LuaAlias));	// [-0,+1]=2
		pAlias->nMagic = LUA_ALIAS_MAGIC;				// Set magic number
		pAlias->pRCB = NULL;							// Create as zombie alias
		lua_pushvalue(pVM, -1);							// [-0,+1]=3 Duplicate alias
		lua_rawsetp(pVM, -3, pRCB);						// [-1,+0]=2 Add new alias to WAT

This code hard crashes in luaH_get() at the last line (lua_rawsetp). The value pRCB is not NULL (it’s just a memory address). If I re-write the code using lua_settable():

		 lua_rawgetp(pVM, LUA_REGISTRYINDEX, (void*) core_RCBAliasCreateLS);	// [-0,+1]=1 Get WAT to TOS 
		LuaAlias *pAlias = (LuaAlias*) lua_newuserdata(pVM, sizeof(LuaAlias));	// [-0,+1]=2
		pAlias->nMagic = LUA_ALIAS_MAGIC;				// Set magic number
		pAlias->pRCB = NULL;							// Create as zombie alias
		lua_pushlightuserdata(pVM, pRCB);				// [-0,+1]=3 Put RCB key on stack
		lua_pushvalue(pVM, -2);							// [-0,+1]=4 Duplicate alias
		lua_settable(pVM, -4);							// [-2,+0]=2 Add new alias to WAT

This code does NOT crash. The table being accessed (the “WAT”) does have a metatable, but it is used only to set the values as weak; it has no __index/__newindex fields. My understanding is that these two blocks of code should be logically equivalent, yet one crashes inside Lua and the other appears to run with no problems. I’ve stared at this till my eyes hurt, and cannot see what could be causing the crash.

Any thoughts anyone?

—Tim