3

从 lua 解析变量时,lua 的行为很奇怪。

C++:

int LuaManager::SetTimer(lua_State *pLua)
{
    if (!lua_isstring(pLua, 0)) throw "expected: string";
    if (!lua_isnumber(pLua, 1)) throw "expected: number";

    std::string callback = lua_tostring(pLua, 0);
    double delay = lua_tonumber(pLua, 1);
    Timer timer = Timer(callback, delay);

    return 0;
}

路亚:

SetTimer("Durp", 10);

我从该行得到“0x76C44598 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x00D7F588 处的字符”

std::string callback = lua_tostring(pLua, 0);

当我调试代码并在弹出异常时按继续时,它会将随机变量抛出到变量中。对于double delay.

然而,当我说:

std::string callback = lua_tostring(pLua, -2);
double delay = lua_tonumber(pLua, -1);

它仍然会给出异常,但会抛出正确的变量。

4

1 回答 1

1

从我的记忆中,这条线

std::string callback = lua_tostring(pLua, 0);

应该

std::string callback = lua_tostring(pLua, 1);

因为 lua 中的索引从 1 开始。

于 2015-01-19T07:01:38.307 回答