lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A simple explanation to the crash is that function "isFunction" is not balanced regarding the Lua stack.
I.e. it pushes one element into the stack and does not pop it afterwards. So in the for loop, when the stacks grows pas its default value (20) the program crashed.

Extract from the doc (ref manual):

"When you interact with the Lua API, you are responsible for ensuring consistency. In particular, you are responsible for controlling stack overflow. You can use the function lua_checkstack to ensure that the stack has extra slots when pushing new elements.

Whenever Lua calls C, it ensures that the stack has at least LUA_MINSTACK extra slots. LUA_MINSTACK is defined as 20, so that usually you do not have to worry about stack space unless your code has loops pushing elements onto the stack."

Jean-Luc

Le 27 sept. 2012 à 07:25, Булычев Михаил <mbul@arqa.ru> a écrit :

> I switched on assertion in lua.conf:
> 
> #define LUA_USE_APICHECK
> #if defined(LUA_USE_APICHECK)
> #include <assert.h>
> #define luai_apicheck(L,o)    { (void)L; assert(o); }
> #else
> #define luai_apicheck(L,o)    { (void)L; }
> #endif
> 
> and wrote small test code in my program
> ...
> int load_err=luaL_loadfile (code, fileName);
>    if (load_err)
>    {
>        sprintf(lastError, "%s", lua_tostring(code, -1));
>        SetState(STATE_STOPPED);
>        ShowError();
>        return 0;
>    }
> 
>    luaL_openlibs(code);
> ...
>    int luaErr = lua_pcall(code, 0, 0, 0);
> ...
>    char func_name[256];
>    bool res = false;
>    for (int i=0; i<30; i++)
>    {
>        sprintf(func_name, "function_%d", i);
>        res = isFunction(code, func_name);
>    }
> ....
> bool isFunction(lua_State *lvm, char *functionName)
> {
>    lua_getglobal (lvm, functionName);
> 
>    int type = lua_type(lvm, -1);
> 
>    if (type != LUA_TNIL && type == LUA_TFUNCTION)
>    {
>        return true;
>    }
>    else
>    {
>        return false;
>    }
> }
> 
> and it always crashes when i reach 20.
> 
> 
>