lua-users home
lua-l archive

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


I get this issue from chinese Lua programmer discussion.

The C client code like following, I use VC2008 to build it, lua5.1.4, the crash will happen in release mode.

/* ----------------------------------------------- */
bool loadfile1(lua_State* LA)
{
    if(!LA)
        return false;   

    lua_State* LB = lua_newthread(LA);

    lua_newtable( LB );    //new globals table
    lua_newtable( LB );    //metatable
    lua_pushliteral( LB, "__index" );
    lua_pushvalue( LB, LUA_GLOBALSINDEX );  //original globals
    lua_settable( LB, -3 );
    lua_setmetatable( LB, -2 );
    lua_replace( LB, LUA_GLOBALSINDEX );    //replace LB's globals

    luaL_dofile( LB, "e:\\test1.lua" );

    lua_pushliteral( LB, "doit" );
    lua_gettable( LB, LUA_GLOBALSINDEX );
    lua_resume( LB, 0 );

    return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
    lua_State *LA = lua_open(); /* opens Lua */
    luaL_openlibs(LA);

    for(int i = 0; i < 500; i++)
    {
        printf("%d\n", i);
        loadfile1(LA);
    }

    return 0;
}

You could change e:\\test1.lua to your folder,
The test1.lua likes:
-------------------------
function doit()
    print( "Script A" )
end
-------------------------


The crash must happen in our test,

Who could tell me, where is the error? is it a bug in Lua?

Another method could cause the crash it, just make the call of lua_newthread().

bool loadfile1(lua_State* LA)
{
    if(!LA)
        return false;   

    lua_State* LB = lua_newthread(LA);
    return true;
}

And it will crash in another place of Lua source code.

Regards,

sagasw

------------------------------------
C++, Lua, living in Dalian
http://sunxiunan.com/
http://twitter.com/sagasw
------------------------------------