Skip to content

Error occurring when storing RefCountedPtr in the global map #324

@hadisi1993

Description

@hadisi1993

Hi!
During the process of using LuaBridge, I encountered a problem. In my C++ code, I set up a global map to store objects of type luabridge::RefCountedPtr:

using testLuaBridgeMap = std::unordered_map<std::string, luabridge::RefCountedPtr<testLuaBridge>>;
testLuaBridgeMap g_Map;

Additionally, I set up a function that, based on the args passed from Lua, checks if the object exists in the map. If it does, it exports the existed object; otherwise, it creates a new object and exports, meanwhile stores it in the map.

int createTestLuaBridge(lua_State* L) {
    // 从堆栈中读取lua table
    luabridge::LuaRef table = luabridge::LuaRef::fromStack(L,-1);
    std::string tempA;
    std::string tempB;
    if (table.isTable())
    {
        if(table["a"].isString())
        {
            tempA = table["a"].cast<std::string>();
        }
        else
        {
            
            luabridge::push(L,"invaild arg: a should be type string");
            luabridge::push(L,luabridge::Nil());
            return 2;
        }
        if(table["b"].isString())
        {
            tempB = table["b"].cast<std::string>();
        }
        else
        {
            luabridge::push(L,"invaild arg: b should be type string");
            luabridge::push(L,luabridge::Nil());
            return 2;
        }
    
        std::string tempC = tempA+tempB;
        if(g_Map.find(tempC) != g_Map.end())
        {
            
            luabridge::push(L,"creating success");
            luabridge::push(L,g_Map[tempC]);
            
            return 2;
        }
        else
        {
            testLuaBridge*rawPtr = new testLuaBridge(tempA,tempB);
            luabridge::RefCountedPtr<testLuaBridge> Ptr(rawPtr); 
            g_Map[tempC] = Ptr;
            
            luabridge::push(L,"creating success");
            luabridge::push(L,Ptr);
            return 2;
        }
    }
    else 
    {
        luabridge::push(L,"invalid arg: should be a lua table");
        luabridge::push(L,luabridge::Nil());
        return 2;
    }
}

I register my class and functions like this.

   luabridge::getGlobalNamespace(L)
       .beginClass<testLuaBridge>("testLuaBridge")
       .addConstructor<void(*)(std::string,std::string),luabridge::RefCountedPtr<testLuaBridge> >()
       .addFunction("print", &testLuaBridge::print)
       .endClass()
       .addFunction("createTestLuaBridge",createTestLuaBridge);

and use it in lua such way:

local msg,ins = createTestLuaBridge({["a"] = "wzq",["b"] = "hello world"});
ins:print()

When the Lua script finishes running, an error happened:

lua: external/LuaBridge/RefCountedPtr.h:195: void luabridge::RefCountedPtr<T>::reset() [with T = testLuaBridge]: Assertion `itCounter != getRefCounts().end()' failed.
Aborted (core dumped)

This seems to be an issue related to the object's lifetime. Can anybody help me take a look?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions