lua-users home
lua-l archive

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


> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br 
> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of 
> jiang.yu(mos)
> Sent: Friday, May 11, 2007 3:13 PM
> To: Lua list
> Subject: Reload a script file with coroutine
> 
> Hi!
>     In my project each script file associated with a lua_state like:
>     std::map<std::string,lua_State*> ScriptMap
>     and the ScriptEngine have a function ClearScriptFile like
>     void ClearScriptFile(string file)
>     {
>         itScript it = m_mapScript.find(file);
>         m_mapScript.erase(it);
>         lua_close(it->second);
>     }
> 
>     Normally it works ok, but if the script use coroutine like:
> 
>     function main()
>          co = coroutine.create(function ()
>          testyield()
>      end)
>      print(coroutine.resume(co))
>     end
> 
>     function testyield()
>      YieldSleep(10000)
>     end
> 
>     YieldSleep is a C function:
>     int YieldSleep(lua_State* L)
>     {
>      int time = lua_tonumber(L,1);
>      AddYieldSleep(L,time); // Add a timer and use L as param.
>      return lua_yield(L,0);
>     }
> 
>     and the OnTimer:
>     DWORD OnTimer(DWORD idTimer)
>     {
>         lua_State* L = (lua_State*)idTimer;
>         lua_resume(L,0);
>     }
> 
>     Then after ClearScriptFile when OnTimer call 
> lua_resume(L,0) the program
> 
> crash.
>     So is there any way to handle it ?
> 
> Best Regard.
> Mos.

I mean when I Close a lua_State L, can I know all coroutine of L, then 
     void ClearScriptFile(string file)
     {
         itScript it = m_mapScript.find(file);
         m_mapScript.erase(it);
        L = it->second;
        //for l  in coroutines of L  , close l, remove timer of l 
         lua_close(L);
     }