lua-users home
lua-l archive

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


I need to remove a lua reference to a C++ pointer object. In wxLua you can create wxWindows widgets that will be destroyed by their parent window by wxWindows. For example, if you create a frame with some buttons, then delete the frame, you now have dangling references to the buttons that lua doesn't know are gone.

The code I use to create a lua reference to a window is below, so I guess what I'm asking for is how do I do the reverse (remove the reference by the pointer's value).

Thanks for any help,
-John Labenski


In a nutshell, the lua code is this
local panel = wx.wxPanel(parent, -1)
The code below just calls lua_newuserdata(..) then lua_setmetatable(..).


static int LUACALL wxPanel_constructor(lua_State *L)
{
    wxPanel *returns;
    // get number of arguments
    int argCount = lua_gettop(L);

.... get parent, id, pos, size, style, name from the stack

    // call constructor
    returns = new wxPanel(parent, id, *pos, *size, style, name);
    // add to tracked window list
    if (wxDynamicCast(returns, wxWindow) != NULL)
        addToTrackedWindowList(L, (wxWindow*)returns);
    // push the constructed class pointer
    pushuserdatatype(L, s_wxPanel, returns );
    // return the number of parameters
    return 1;
}

// Push a data type onto the stack and set its tag
void LUACALL pushuserdatatype(lua_State *L, int iTag, const void *data)
{
    if (data != NULL)
        tpushusertag(L, data, iTag);
    else
        lua_pushnil(L);
}

void LUACALL tpushusertag(lua_State *L, const void *u, int tag)
{
  const void **ptr = (const void **) lua_newuserdata(L, sizeof(void *));
  if (ptr != NULL)
  {
      *ptr = u;
      if (tag != TLUA_NOTAG && tget(L, tag))
      {
          if (lua_setmetatable(L, -2) == 0)
              terror(L, "wxLua: Unable to set metatable");
      }
  }
  else
      terror(L, "wxLua: Out of memory");
}