[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Memory leak with lua ...
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Mon, 23 Apr 2007 19:58:26 -0400
Saravanan Palanichami wrote:
> I seem to have a problem with memory in lua ...
>
> This piece of code seems suspect -->
>
> bool Lua::RunString(const std::string &Command) {
> //Put error handler on stack
> lua_pushliteral(m_pScriptContext, "OnError");
> lua_rawget(m_pScriptContext, LUA_GLOBALSINDEX);
> int errorFunc = lua_gettop(m_pScriptContext);
>
> int retCode = luaL_loadbuffer(m_pScriptContext,
> Command.c_str(), Command.size(), NULL);
>
> if (retCode)
> {
> Log(ELOGSEV_CRITICAL, lua_tostring(m_pScriptContext,
-1));
> return false;
> }
>
> int callRetCode =
> lua_pcall(m_pScriptContext, 0, LUA_MULTRET, errorFunc);
> if (callRetCode)
> {
> // Report runtime error message
> if(callRetCode != LUA_ERRRUN)
> Log(ELOGSEV_CRITICAL,
lua_tostring(m_pScriptContext, -1));
> return false;
> }
> return true;
> }
>
> a) This just runs a command on the lua state
> b) When I run this over and over again, if I look at the totalbytes
> field in the lua state, it grows slowly. It does not increase for
> every run but does it every 10 or maybe 20 calls. c) I am using LUA
> 5.1 on a fedora core 2 box.
As said before it's probably not Lua itself that is leaking, but rather
your scripts that create Lua objects without releasing them.
With the above code, you don't make sure that the stack size is balanced
(that is it doesn't necessarily have the same size when entering the
RunString function and when leaving it). If your Command snippet is
returning multiple values, LUA_MULTRET let them on the stack, and your
stack simply grow (the GC cannot collect things on the stack). So make
sure you clean your stack properly according to your Command return
values (either in RunString or in the function calling RunString).
The other possible leak can come from an eventual side effect of your
Command. If the command allocates data in the globals table (that is if
it creates entries in _G, or any table in _G, or in any table in one of
these tables, etc.), then that data is not collectible, and you have a
leak. Give us a sample of your Lua Commands and we will tell you if they
may leak.