darealcore
darealcore

Reputation: 11

C++ Call Lua Functions

I'm just about to include Lua in my project. Only have one problem, if I link my own class and create it in Lua, the stack is not cleaned up and I get memory leaks. The Memory Go Up and Up.

MyClass:

class CTest
{
public:
    CTest(std::string s)
        : m_s(s)
    {
        std::cout << s << std::endl;
    }

    ~CTest()
    {

    }

private:
    std::string m_s;
};

C++ Test Code:

    auto state = luaL_newstate();
    luaL_openlibs(state);

    luabridge::getGlobalNamespace(state)
        .beginClass<CTest>("Test")
            .addConstructor<void(*)(std::string)>()
        .endClass();

    int iState = luaL_dofile(state, "Test.lua");

    while (true)
    {
        int nStatus = 0;

        lua_getglobal(state, "test");
        nStatus = lua_pcall(state, 0,0,0);
    }

Lua Code

local ii = 0

function test()
    local i = Test("Hallo " .. ii)
    ii = ii + 1
end

I'm using Lua 5.2.0.

Upvotes: 1

Views: 178

Answers (1)

I can reproduce your issue on Lua 5.2.0, but not on Lua 5.2.1 or any newer versions. My conclusion is that it's just a bug in versions of Lua prior to 5.2.1. Just update to a modern version of Lua and you shouldn't have the problem either.

Upvotes: 1

Related Questions