lua-users home
lua-l archive

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


Yeah, i agree, it is propably not the stack.

I can see one possible problem: since you use dostring, the string will be
compiled every time - which creates a new executable chunk for each call,
which trashes the gc at best or even may be responsible for your growing
memory size.

if you're already using lua 4.1, have a look at lua_loadbuffer to prevent
that.

otherwise you can use a little trick someone posted on this list:

put a "return function()" in front of your script string and an "end" behind
it.
now you can dostring() this modified string and get as a result a lua
function which is the equivalent to your original dostring(). you can use
this function value for lua_call()ing your script as many times as you like,
without compiling it every time. (just make sure it stays on the stack by
calling lua_pushvalue to duplicate it before you use up one value in the
lua_call)

i hope this helps,
Peter

-----Original Message-----
From: owner-lua-l@tecgraf.puc-rio.br
[mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Byron Guernsey
Sent: Tuesday, October 23, 2001 11:15 PM
To: Multiple recipients of list
Subject: Preventing memory leaks


I use Lua in a game system used for multiplayer web games (
http://www.bingonet.net/ )

I've noticed the memory footprint of the game engine grows with time
and the games that execute Lua scripts frequently, grow much more
quickly in size. The rest of the code has been checked extensively
with BoundsChecker and has no notable memory leaks.

In using Lua, I followed the example from the min.c code to write a
minimal interpretter.  I initialize it with a few of my own global
functions one time.  I instantiate one Lua VM and use it throughout
the lifetime of the server process.

My code to execute a text script looks something like:


int Game::ScriptExecute(char *string)
{
 int result = 0;

 ScriptSetGlobals(); // set global variables in the vm
 result = lua_dostring (Lua, string);
 if(result == 0) {
// Get the first number off the stack
result = (int) lua_gettop(Lua);
if(result == 1) {
// Retrieve the return value
result = (int) lua_tonumber(Lua, 1);
lua_pop(Lua, 1);

// Get all Readable global variables in case they have
// been modified
ScriptGetGlobals();
} else {
ODS("ERROR: Top of stack reported as %d\n", result);
result = -1;
}
 } else {
ODS("ERROR: Script failed with code %d\n", result);
result = -1;
 }
 return result;
}

Seems simple enough.  My ScriptSetGlobals() and ScriptGetGlobals()
export some game variables into the script before each execution.

they use several calls similar to:

 lua_pushnumber(Lua, (double) PointValue);
 lua_setglobal(Lua, "PointValue");

and

 lua_getglobal(Lua, "PointValue");
 PointValue = (int) lua_tonumber(Lua, 1);
 lua_pop(Lua, 1);

to set/get globals within the VM before/after execution.

thats pretty much it..but I'm definitely attributing memory leakage
to that script execution routine.  I'm thinking of adding the
gettop/settop wrapped around the ScriptExecute() routine to
save/restore the stack, but something tells me thats not the problem.

Would you guys happen to have any suggestions?  Is there something
I'm obviously doing wrong?

Thanks,
Byron