How to free memory allocated by lua_newuserdata using delete operator?

How to free memory allocated lua_newuserdata?

I have a class with a name Foo, and this class has a constructor and desrector, and I need to execute both, but I don’t know how to use the C ++ operator delete, because I did not use newto allocate memory.

I tried to do this in a new Lua function that will create an object:

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

and in gc function I tried:

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

But I got a segmentation error.

+3
source share
3 answers

In this case, you need to use the lua concept called userdatum, which means you need to highlight the pointer to your object using 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;
}
+8

, C++ , . lua_newuserdata, , Lua, "__gc" Lua , " pMyObject-> ~ MyClass() ". ( lua_newuserdata) Lua, delete "__gc" . Lua, Lua.

0

, lua_newuserdata , Lua . , userdatum, Lua, .

userdatum , __gc; , . , :

// Constructor.
int lua_Timer_new(lua_State* L) {
    void** vpp;
    vpp = reinterpret_cast<void**>(lua_newuserdata(L, sizeof(Timer*)));
    *vpp = new Timer();
    luaL_setmetatable(L, LUA_TIMER);
    return 1;
}

// Destructor corresponding to the __gc metamethod.
int lua_Timer_delete(lua_State* L) {
    Timer* t = *reinterpret_cast<Timer**>(luaL_checkudata(L, 1, LUA_TIMER));
    delete t;
    return 0;
}

( - , .. --would .)

0

All Articles