lua-users home
lua-l archive

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


Hi,

I’m a programmer at a game programming studio. We are using Lua for scripting language. During the testing of our product we have stumbled on the following problem : we found that a reference to the ‘hook table’(usually kept in the registry in the pair KEY_HOOK = ‘hook table’) is stored on the stack of a thread that has been recently interrupted by error(trying to get the length of boolean value). Unfortunatly I can’t provide you with simple lua code that reproduces this situation. I found that one of the possible causes for this problem is in the ‘hookf’ function. It pushes the ‘hook table’ on the stack and then it never pops it. In almost all cases this is ok as the stack is restored afterwards. But in some weird situations involving thread and unrecoverable errors, it seems to be left behind. Here is the function in question plus the suggested change.

 

static void hookf (lua_State *L, lua_Debug *ar) {

  static const char *const hooknames[] =

    {"call", "return", "line", "count", "tail return"};

  lua_pushlightuserdata(L, (void *)&KEY_HOOK);

  lua_rawget(L, LUA_REGISTRYINDEX);

  lua_pushlightuserdata(L, L);

  lua_rawget(L, -2);

+ lua_remove(L, -2);

  if (lua_isfunction(L, -1)) {

    lua_pushstring(L, hooknames[(int)ar->event]);

    if (ar->currentline >= 0)

      lua_pushinteger(L, ar->currentline);

    else lua_pushnil(L);

    lua_assert(lua_getinfo(L, "lS", ar));

    lua_call(L, 2, 0);

  }

}

 

I suggest that there may be another bug that let the forgotten value on the stack. In our case the bug became headache because at some point this thread got saved, which resulted in a large memory leak on load, due to the lack of handling of the registry table by the save/load code.

 

P.S. Can somebody please tell me why the content of the ‘hook table’ is ever garbage collected? I’m 99% sure it’s collected but I can’t seem to understand why or where?