【问题标题】:lua_pcall cause "cannot resume dead coroutine"lua_pcall 导致“无法恢复死协程”
【发布时间】:2013-05-03 10:55:37
【问题描述】:

我下面有这样的c++代码来调用lua代码

for (int i =0; i < 2000; i++)
{
    lua_getglobal(g_L, "AnalyzeScript");
    lua_pushstring(g_L, "1");
    lua_pushstring(g_L, "2");
    lua_pushstring(g_L, "3");

    if(lua_pcall(g_L,3,0,0) != 0)
    {
        //          char temp[200]={0}; sprintf(temp, "err: %s",  lua_tostring(g_L, -1));
        //          MessageBoxA(0,temp,0,0);
    }

lua 代码如下

local cnt = 0
function AnalyzeScript(foldername, filename, pOut)
    cnt = cnt + 1
    print(cnt)
end

一切正常,除了“不能恢复死协程”(哪个协程在别的地方)

调用 lua func 看起来像 2000 次破坏了 lua 堆栈,如果我将 2000 更改为 200,则一切正常!

为什么?

【问题讨论】:

  • 什么是“协程”?你能给我们enough code to be able to see this happen吗?
  • 我知道我的任务还不够,我确实在钩子函数中调用了 for,该函数在计时器中执行,我无法准确描述该函数是什么,因为我没有觉得很特别

标签: lua


【解决方案1】:

我无法重现您的错误。我稍微修改了代码:

/* test.c */
#include "lua.h"
#include "lauxlib.h"

void main() {
    int i;
    lua_State *g_L = luaL_newstate();
    luaL_openlibs(g_L);
    luaL_dofile(g_L, "s.lua");
    for (i =0; i < 2000; i++)
    {
        lua_getglobal(g_L, "AnalyzeScript");
        lua_pushstring(g_L, "1");
        lua_pushstring(g_L, "2");
        lua_pushstring(g_L, "3");

        if(lua_pcall(g_L,3,0,0) != 0)
        {
//          char temp[200]={0}; sprintf(temp, "err: %s",  lua_tostring(g_L, -1));
//          MessageBoxA(0,temp,0,0);
        }
    }
    lua_close (g_L);
}

它是在我的 Linux 中使用以下命令编译的(我在 /fakepath/lua-5.2.0 中安装了 Lua 发行版):

gcc test.c -I/fakepath/lua-5.2.0/src /fakepath/lua-5.2.0/src/liblua.a -lm -ldl

文件 test.lua 正是您发布的内容。 也许问题出在其他地方......

【讨论】:

  • 我知道我的任务还不够,我确实在钩子函数中调用了 for,该函数在计时器中执行,我无法准确描述该函数是什么,因为我没有觉得很特别
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
相关资源
最近更新 更多