lua-users home
lua-l archive

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


On Sat, Apr 7, 2012 at 3:21 PM, Coda Highland <chighland@gmail.com> wrote:
> You'd still need to use operator delete on it instead of free if you
> need the destructor invoked.

You can explicitly invoke the destructor from the __gc metamethod:

int factory(lua_State* L)
{
  T* object = new (reinterpret_cast<T*>(lua_newuserdata(L, sizeof(T)))) T;
  /* set metatable here */
  return 1;
}

int gc_metamethod(lua_State* L)
{
  T* object = reinterpret_cast<T*>(lua_touserdata(L, 1));
  object->~T();
  return 0;
}