5

如何释放分配的内存lua_newuserdata

我有一个类叫做Foo,这个类有构造函数和析构函数,我都需要执行,但是我不知道如何使用 C++ 运算符delete,因为我没有使用new分配内存。

我尝试在创建对象的 Lua 函数 new 中这样做:

Foo *pf = reinterpret_cast<Foo *>(
                lua_newuserdata(L, sizeof(Foo)));

在 gc 函数中,我尝试了:

Foo *foo = reinterpret_cast<Foo *>(lua_touserdata(L, 1));
delete foo;

但是我遇到了分段错误。

4

2 回答 2

9

在这种情况下,您需要使用一个名为 userdatum 的 lua 概念,这意味着您需要使用 lua_newuserdata 为您的对象分配一个指针。

要分配内存,请执行以下操作:

Foo **pfoo = reinterpret_cast<Foo **>(lua_newuserdata(L, sizeof(Foo*)));
*pfoo = new Foo(foo);

在垃圾收集器功能中,您可以这样做:

Foo **foo = reinterpret_cast<Foo **>(lua_touserdata(L, 1));
delete *foo;

下面是一个使用 userdatum 概念的完整代码示例

#define FOO     "foo"

class Foo {
public:
  Foo(const char *name) {
    this->name = (char *) malloc(strlen(name) + 1);
    strncpy(this->name, name, strlen(name));
  }

  Foo(const Foo &obj) {
    this->name = (char *) malloc(strlen(name) + 1);
    strncpy(this->name, obj.name, strlen(obj.name));
  }

  const char* get_name() const {
    return this->name;
  }

  ~Foo() {
    free(this->name);
  }
private:
  char *name;
};

static Foo* push_foo(lua_State *L, Foo foo) {

  Foo **pfoo = reinterpret_cast<Foo **>(
                lua_newuserdata(L, sizeof(Foo*)));
  *pfoo = new Foo(foo);

  luaL_getmetatable(L, FOO);
  lua_setmetatable(L, -2);

  return *pfoo;
}

static Foo* chk_foo(lua_State *L, int index) {
  Foo *foo;
  luaL_checktype(L, index, LUA_TUSERDATA);
  foo = *reinterpret_cast<Foo **>(luaL_checkudata(L, index, FOO));
  if (foo == NULL)
    luaL_error(L, "error");

  return foo;
}

static int foo_new(lua_State *L) {
  int argc = lua_gettop(L);

  if(argc != 1)
    luaL_error(L, "string argument expected");

  const char* str = luaL_checkstring(L, 1);

  push_foo(L, Foo(str));

  luaL_getmetatable(L, FOO);
  lua_setmetatable(L, -2);
  std::cout << "Lua object created!" << std::endl;
  return 1;
}

static int foo_get(lua_State *L) {
  Foo *foo = chk_foo(L, 1);
  luaL_argcheck(L, foo != NULL, 1, "Error foo");

  lua_pushstring(L, foo->get_name());
  return 1;
}

static int foo_gc(lua_State *L) {
  Foo **foo = reinterpret_cast<Foo **>(lua_touserdata(L, 1));
  luaL_argcheck(L, *foo != NULL, 1, "Error foo");
  delete *foo;
  std::cout << "Lua GC executed!" << std::endl;
  return 0;
}

int luaopen_foolib(lua_State *L) {
  static const luaL_Reg Obj_lib[] = {
    { "get", &foo_get },
    { NULL, NULL }
  };

  static const luaL_Reg LuaLib_Foo[] = {
    { "new", &foo_new },
    { NULL, NULL }
  };

  luaL_newlib(L, LuaLib_Foo);

  // Stack: MyLib
  luaL_newmetatable(L, FOO);
  luaL_newlib(L, Obj_lib);
  lua_setfield(L, -2, "__index");

  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, foo_gc);
  lua_settable(L, -3);
  lua_pop(L, 1);

  return 1;
}

int main(int argc, char **argv) {
  lua_State *L;
  L = luaL_newstate();

  luaL_openlibs(L);

  luaL_requiref(L, "foo", &luaopen_foolib, 1);
  lua_pop(L, 1);

  const char *code = "f = foo.new(\"my_test\")\nprint(f:get())";

  if(luaL_loadstring(L, code) != 0)
  {
    std::cout << "Could not load: " << argv[1] << std::endl;
    exit(EXIT_FAILURE);
  }

  if(lua_pcall(L, 0, 0, 0) != 0)
  {
    std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
    lua_pop(L, 1);
    exit(EXIT_FAILURE);
  }
  lua_close(L);
  return 0;
}
于 2014-02-15T02:35:41.463 回答
3

C++ 删除对象有两个步骤,运行析构函数和释放动态分配的内存。如果调用 lua_newuserdata 为对象分配存储空间,则可以使用放置 new 使用 Lua 分配的内存空间运行构造函数,并且可以使用 Lua 垃圾收集“__gc”方法显式调用对象的析构函数,例如“ pMyObject->~MyClass ()”。但是,存储回收(释放 lua_newuserdata 动态分配的内存)是由 Lua 环境自动完成的,因此在“__gc”方法中调用 delete 将导致您的软件失败。如果内存是由 Lua 分配的,那么它也应该由 Lua 一致地释放。

于 2019-06-27T15:16:47.107 回答