Lua C API不适用于指针

问题描述 投票:2回答:1

这就是我做的。我在C ++代码中创建了一个struct和一个静态函数

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;
}

我还在lua中注册了该函数并初始化了一个ItemDefination对象。

ItemDefinition def;
def.price = 120;
lua_pushlightuserdata(lua_state_, (ItemDefinition*)&def);
lua_setglobal(lua_state_, "def");
luaL_Reg module[] = {{"getPrice", &getPrice}, {NULL, NULL}};

从我的Lua脚本中,我只是简单地调用该函数

getPrice(def)

如果我打印出def的地址,如果工作正常。所以我确信该函数被成功调用。但是,如果我想要获得def->price,我将从地址消毒剂"==283789==ERROR stack use after return"得到一个错误我不熟悉。你能帮忙解决问题所在吗?

c++ c lua
1个回答
0
投票

解决问题,NVM。这实际上是因为指针问题。

© www.soinside.com 2019 - 2024. All rights reserved.