This is what I did. I created a struct and a static function in my C++ code
struct ItemDefinition {
int64_t price;
};
int static getPrice(lua_State* L) {
ItemDefinition* def = (ItemDefinition*)lua_touserdata(L, 1);
printf("%p\n", def);
printf("%d\n", def->price);
return 0;
}
I also registered the function in lua and initialized an ItemDefination object.
ItemDefinition def;
def.price = 120;
lua_pushlightuserdata(lua_state_, (ItemDefinition*)&def);
lua_setglobal(lua_state_, "def");
luaL_Reg module[] = {{"getPrice", &getPrice}, {NULL, NULL}};
from my Lua script, I just simply call the function
getPrice(def)
if works fine if I just print out the def's address. So I'm sure the function get called successfully. However, if I'm trying to get def->price
, I will get an error from address sanitizer "==283789==ERROR stack use after return"
I'm not familiar with both c++ and lua. Could you please help where the problem could be?