Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua arguments at wrong memory location

When parsing variables from lua, lua is acting strangely.

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;
}

lua:

SetTimer("Durp", 10);

I get a "First-chance exception at 0x76C44598: Microsoft C++ exception: char at memory location 0x00D7F588" from the line

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

When I debug the code and press continue when the exception pops up it throws random variables into the variable. The same goes for the double delay.

However when I say:

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

It will still give the exception but the correct variables are thrown in.

like image 224
R4VANG3R Avatar asked May 03 '26 23:05

R4VANG3R


1 Answers

From my memories, the line

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

should be

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

because indexes in lua start at 1.

like image 132
vincentp Avatar answered May 05 '26 13:05

vincentp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!