lua-users home
lua-l archive

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


Hello,

I'm running into some memory problems while using LuaInterface.  My
test program looks like this:

  Lua lua = new Lua();
  lua.OpenBaseLib();
  lua.OpenTableLib();

  for (int i=0;;i++) {
    LuaTable tbl = lua.GetTable("table");
    i++
  }

It isn't important which table I call GetTable on, I'm just using
"table" for the example.  After anywhere between 2000 and 15000
iterations, something breaks.  Sometimes it is an invalid cast
exception, sometimes it is a null reference exception.  In larger
programs, I would even get syntax errors where there weren't any
before.  In that case, it seems like the Lua stack was being
corrupted.  The general idea seems to be that memory is getting messed
up.

Each call to lua.GetTable() is eventually calling lua_ref, so we are
creating a lot of references.  But I would expect the C# garbage
collector to run every now and then and call the LuaTable destructor
which would call lua_unref.

There are obviously extra layers between lua.GetTable and the Lua API
calls, but (unless I am mistaken) it basically boils down to this C
equivalent:

  for(i=0;;i++) {
    lua_getglobal(L, "table");
    ref = luaL_ref(L, LUA_REGISTRYINDEX);
    lua_getref(L, ref);
    lua_pop(L, -1);
  }

I ran this using the same Lua DLL that LuaInterface is using, yet this
code doesn't fail like the LuaInterface code does.  When running this
C version, luaL_unref isn't even being called, so I don't think it is
related to the number of references being created.  So the problem
seems to be in the translation layer that LuaInterface creates.  But I
cannot see anything in the LuaInterface code that would cause this
problem.

Any ideas or advice are welcome.  

-- 
Zachary P. Landau <kapheine@gmail.com>