Lua C API嵌套表seg。故障

时间:2014-07-31 08:25:51

标签: c++ arrays lua lua-table

我想用Lua C API创建一个简单的嵌套表。表格填满了mysql结果。但是,当我尝试阅读该表时,我的应用程序崩溃了。

代码:

    int i = 0;
    lua_newtable(L);
    while(row = mysql_fetch_row(result))
    {
        lua_newtable(L);
        lua_pushliteral(L, "event");
        lua_pushnumber(L, atoi(row[0]));
        lua_pushliteral(L, "timestamp");
        lua_pushnumber(L, atoi(row[1]));
        lua_settable(L, -5);
        lua_rawseti(L, -2, ++i);
    }

上面应该会产生一个Lua表:

{
     {event = 1, timestamp = 1234567890},
     {event = 2, timestamp = 1234567890},
     {event = 2, timestamp = 1234567890},
     [..]
}

GDB回溯代码段:

(gdb) bt
#0  luaH_getnum (t=0x3c7db040, key=1) at ltable.c:444
#1  0x0825f94e in luaH_setnum (L=0x3c7d5ca0, t=0x3c7db040, key=1) at ltable.c:500
#2  0x08257fd5 in lua_rawseti (L=0x3c7d5ca0, idx=-2, n=1) at lapi.c:593

这里有什么不对?

1 个答案:

答案 0 :(得分:3)

您的代码只调用lua_settable一次,但您希望将"event""timestamp"添加到表中:

int i = 0;
lua_newtable(L);
while(row = mysql_fetch_row(result))
{
    lua_newtable(L);
    lua_pushliteral(L, "event");
    lua_pushnumber(L, atoi(row[0]));
    lua_settable(L, -3); //Set event
    lua_pushliteral(L, "timestamp");
    lua_pushnumber(L, atoi(row[1]));
    lua_settable(L, -3); //changed `-5` to `-3`
    lua_rawseti(L, -2, ++i);
}

您可以使用lua_setfield

简化代码
int i = 0;
lua_newtable(L);
while(row = mysql_fetch_row(result))
{
    lua_newtable(L);
    lua_pushnumber(L, atoi(row[0]));
    lua_setfield(L,-2,"event");
    lua_pushnumber(L, atoi(row[1]));
    lua_setfield(L,-2,"timestamp");
    lua_rawseti(L, -2, ++i);
}

最后,确保您有luaL_checkstack

的足够工作堆栈
luaL_checkstack(L,3,nullptr);
int i = 0;
lua_newtable(L);
while(row = mysql_fetch_row(result))
{
    lua_newtable(L);
    lua_pushnumber(L, atoi(row[0]));
    lua_setfield(L,-2,"event");
    lua_pushnumber(L, atoi(row[1]));
    lua_setfield(L,-2,"timestamp");
    lua_rawseti(L, -2, ++i);
}