lua-users home
lua-l archive

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

I use Lua in a game system used for multiplayer web games ( )

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
} 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");


 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?