problem with luaL_newmetatable

dolphindolphin Join Date: 2010-04-23 Member: 71502Members Posts: 2
I have a problem that is causing me to run out of memory when I have Decoda attached. It looks like LuaInject is calling luaL_ref when I call luaL_newmetatable, but never calling luaL_unref. Each call to luaL_newmetatable is adding a new reference to the metatable to the registry. This causes the registry table to grow unbounded.

This feels like a bug in handling luaL_newmetatable. According to the docs, luaL_newmetatable doesn't always create a new metatable, it will return an existing one. I think I can work around this by using luaL_getmetatable then checking for nil then calling luaL_newmetatable, but I hesitate to add in strange looking code just to make Decoda work. If you have any other suggestions please let me know.

Comments

  • dolphindolphin Join Date: 2010-04-23 Member: 71502Members Posts: 2
    I have been able to work around the problem with the following code:

    CODE
    inline int newmetatable(lua_State* L, const char* name)
    {
        luaL_getmetatable(L,name);
        if(lua_isnil(L, -1))
        {
            lua_pop(L, 1);
            luaL_newmetatable(L,name);
            return 1;
        }
        return 0;
    }

    #define luaL_newmetatable(L,name) newmetatable(L,name)
Sign In or Register to comment.