使用lua协程API和lua_close时发生分段错误

我正在开发一个 Lua 脚本中断项目,我想使用 std::Stack 和 lua 协程来保存上下文。但是,当我将 stacksize 设置为超过 38 时,它会在 lua_resume 和 lua_close 中随机崩溃。

test.lua:

local stacksize = 40 --将 stacksize 更改为小于 30 时,它可以正常运行。
function heavy_function(i)
    print("heavy_function start",i)
    if i < stacksize then
        coroutine.yield(i+1)
    end
    print("heavy_function end",i)
end

main.cpp:

#ifdef __cplusplus
extern "C" {
#endif
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#ifdef __cplusplus
}
#endif

#include <iostream>
#include <unistd.h>
#include <time>
#include <stdio.h>
#include <string>
#include <stack>
using namespace std;

int main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);

    int ret = luaL_dofile(L, "test.lua");
    if (ret != 0)
    {
        // 加载脚本时发生错误。返回。
        printf("luaL_dofile error \n");
        return -1;
    }

    // 添加一个计数钩子,在 "count" 次指令之后触发
    //lua_sethook(L, LUAHook, LUA_MASKLINE, 0);

    stack<lua_State *> Lstack;
    Lstack.push(lua_newthread(L));
    int init = 1;
    do{
        lua_getglobal(Lstack.top(), "heavy_function");
        lua_pushinteger(Lstack.top(),init);
        ret = lua_resume(Lstack.top(),L,1);
        if(ret == LUA_YIELD)
        {
            init = luaL_checkinteger(Lstack.top(),-1);
            Lstack.push(lua_newthread(L));
        }
        else if(ret == 0)
        {
            //lua_close(Lstack.top());
            lua_gc(L,LUA_GCCOLLECT,0);
            cout<<"Memory Usage:"<<lua_gc(L,LUA_GCCOUNT,0)<<endl;
            Lstack.pop();
        }
        else{
            cout<<"error"<<endl;
            return -1;
        }
    }while(Lstack.size()>0);
    //printf("lua script interrupted \n");
    lua_close(L);
    return 0;
}

编译器选项:

g++ -g main.cpp -o test -llua -ldl

我怀疑我在调用 lua_newthread 时犯了一个错误。因此,在调用 lua_newstate 之前进行了一个堆栈检查,之后它变得正常了。

if(ret == LUA_YIELD)
{
    init = luaL_checkinteger(Lstack.top(),-1);
    Lstack.push(lua_newthread(L));
    cout<<"lua_checkstack(L,10) = "<<lua_checkstack(L,1)<<endl;//在第47行添加一行
}

想知道我是否犯了一个错误,以及如何改正它?

点赞
用户5675002
用户5675002

你通过不断生成新的 Lua 线程并将它们的 Lua 对象留在堆栈中,导致 Lua 堆栈溢出。

lua_newstack() 不仅返回一个指向 lua_State 结构的指针,还在你的 L 状态的堆栈上留下了一个类型为 LUA_TTHREAD 的值。你应该相应地调整 Lua 堆栈,或以其他方式管理返回的 Lua 线程。

一个快速而肮脏的“fix”是在你的 Lstack.push(lua_newthread(L)); 行之前调用 lua_checkstack(L, 10);。它允许你的代码像原样运行,但堆栈将不断增长。相反,你应该从堆栈中获取新的线程对象并将其放入某个 Lua 表中,直到删除它的时间到来。

2018-09-29 10:27:57