git glue memory leaks
- fix stack imbalances - call panic() instead of nhl_error() if L==0 to prevent immediate crash - drop unused argument prep in get_nh_lua_variables - change nhl_pcall_handle to clean up stack after calling impossible() - don't force full garbage collection runs - don't collect garbage to do memory usage checking - remove unnecessary stack resets
This commit is contained in:
34
src/nhlua.c
34
src/nhlua.c
@@ -165,16 +165,15 @@ l_nhcore_call(int callidx)
|
|||||||
|
|
||||||
lua_getfield(gl.luacore, -1, nhcore_call_names[callidx]);
|
lua_getfield(gl.luacore, -1, nhcore_call_names[callidx]);
|
||||||
ltyp = lua_type(gl.luacore, -1);
|
ltyp = lua_type(gl.luacore, -1);
|
||||||
|
lua_remove(gl.luacore, -2); /* nhcore_call_names[callidx] */
|
||||||
|
lua_remove(gl.luacore, -2); /* nhcore */
|
||||||
if (ltyp == LUA_TFUNCTION) {
|
if (ltyp == LUA_TFUNCTION) {
|
||||||
lua_remove(gl.luacore, -2); /* nhcore_call_names[callidx] */
|
|
||||||
lua_remove(gl.luacore, -2); /* nhcore */
|
|
||||||
nhl_pcall_handle(gl.luacore, 0, 1, "l_nhcore_call", NHLpa_panic);
|
nhl_pcall_handle(gl.luacore, 0, 1, "l_nhcore_call", NHLpa_panic);
|
||||||
} else {
|
} else {
|
||||||
/*impossible("nhcore.%s is not a lua function",
|
/*impossible("nhcore.%s is not a lua function",
|
||||||
nhcore_call_names[callidx]);*/
|
nhcore_call_names[callidx]);*/
|
||||||
nhcore_call_available[callidx] = FALSE;
|
nhcore_call_available[callidx] = FALSE;
|
||||||
}
|
}
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DISABLE_WARNING_UNREACHABLE_CODE
|
DISABLE_WARNING_UNREACHABLE_CODE
|
||||||
@@ -1108,7 +1107,7 @@ nhl_variable(lua_State *L)
|
|||||||
const char *key;
|
const char *key;
|
||||||
|
|
||||||
if (!gl.luacore) {
|
if (!gl.luacore) {
|
||||||
nhl_error(L, "nh luacore not inited");
|
panic("nh luacore not inited");
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1116,7 +1115,6 @@ nhl_variable(lua_State *L)
|
|||||||
lua_getglobal(gl.luacore, "nh_lua_variables");
|
lua_getglobal(gl.luacore, "nh_lua_variables");
|
||||||
if (!lua_istable(gl.luacore, -1)) {
|
if (!lua_istable(gl.luacore, -1)) {
|
||||||
impossible("nh_lua_variables is not a lua table");
|
impossible("nh_lua_variables is not a lua table");
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1141,7 +1139,6 @@ nhl_variable(lua_State *L)
|
|||||||
nhl_pcall_handle(L, 0, 1, "nhl_variable-1", NHLpa_panic);
|
nhl_pcall_handle(L, 0, 1, "nhl_variable-1", NHLpa_panic);
|
||||||
} else
|
} else
|
||||||
nhl_error(L, "Cannot get variable of that type");
|
nhl_error(L, "Cannot get variable of that type");
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
return 1;
|
return 1;
|
||||||
} else if (argc == 2) {
|
} else if (argc == 2) {
|
||||||
/* set nh_lua_variables[key] = value;
|
/* set nh_lua_variables[key] = value;
|
||||||
@@ -1171,7 +1168,6 @@ nhl_variable(lua_State *L)
|
|||||||
nhl_pcall_handle(gl.luacore, 0, 0, "nhl_variable-3", NHLpa_panic);
|
nhl_pcall_handle(gl.luacore, 0, 0, "nhl_variable-3", NHLpa_panic);
|
||||||
} else
|
} else
|
||||||
nhl_error(L, "Cannot set variable of that type");
|
nhl_error(L, "Cannot set variable of that type");
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
return 0;
|
return 0;
|
||||||
} else
|
} else
|
||||||
nhl_error(L, "Wrong number of arguments");
|
nhl_error(L, "Wrong number of arguments");
|
||||||
@@ -1185,28 +1181,19 @@ get_nh_lua_variables(void)
|
|||||||
char *key = NULL;
|
char *key = NULL;
|
||||||
|
|
||||||
if (!gl.luacore) {
|
if (!gl.luacore) {
|
||||||
nhl_error(gl.luacore, "nh luacore not inited");
|
panic("nh luacore not inited");
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_getglobal(gl.luacore, "nh_lua_variables");
|
|
||||||
if (!lua_istable(gl.luacore, -1)) {
|
|
||||||
impossible("nh_lua_variables is not a lua table");
|
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_getglobal(gl.luacore, "get_variables_string");
|
lua_getglobal(gl.luacore, "get_variables_string");
|
||||||
if (lua_type(gl.luacore, -1) == LUA_TFUNCTION) {
|
if (lua_type(gl.luacore, -1) == LUA_TFUNCTION) {
|
||||||
if (nhl_pcall_handle(gl.luacore, 0, 1, "get_nh_lua_variables",
|
if (nhl_pcall_handle(gl.luacore, 0, 1, "get_nh_lua_variables",
|
||||||
NHLpa_impossible)) {
|
NHLpa_impossible)) {
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
key = dupstr(lua_tostring(gl.luacore, -1));
|
key = dupstr(lua_tostring(gl.luacore, -1));
|
||||||
}
|
}
|
||||||
lua_settop(gl.luacore, 0);
|
lua_pop(gl.luacore, 1);
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1244,7 +1231,6 @@ restore_luadata(NHFILE *nhfp)
|
|||||||
luaL_loadstring(gl.luacore, lua_data);
|
luaL_loadstring(gl.luacore, lua_data);
|
||||||
free(lua_data);
|
free(lua_data);
|
||||||
nhl_pcall_handle(gl.luacore, 0, 0, "restore_luadata", NHLpa_panic);
|
nhl_pcall_handle(gl.luacore, 0, 0, "restore_luadata", NHLpa_panic);
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* local stairs = stairways(); */
|
/* local stairs = stairways(); */
|
||||||
@@ -1527,11 +1513,10 @@ nhl_callback(lua_State *L)
|
|||||||
const char *fn, *cb;
|
const char *fn, *cb;
|
||||||
|
|
||||||
if (!gl.luacore) {
|
if (!gl.luacore) {
|
||||||
nhl_error(L, "nh luacore not inited");
|
panic("nh luacore not inited");
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc == 2 || argc == 3) {
|
if (argc == 2 || argc == 3) {
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
rm = FALSE;
|
rm = FALSE;
|
||||||
@@ -1560,7 +1545,6 @@ nhl_callback(lua_State *L)
|
|||||||
lua_pushstring(gl.luacore, cb);
|
lua_pushstring(gl.luacore, cb);
|
||||||
lua_pushstring(gl.luacore, fn);
|
lua_pushstring(gl.luacore, fn);
|
||||||
nhl_pcall_handle(gl.luacore, 2, 0, "nhl_callback", NHLpa_panic);
|
nhl_pcall_handle(gl.luacore, 2, 0, "nhl_callback", NHLpa_panic);
|
||||||
lua_settop(gl.luacore, 0);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1933,7 +1917,6 @@ nhl_pcall(lua_State *L, int nargs, int nresults, const char *name)
|
|||||||
nud->name, ic);
|
nud->name, ic);
|
||||||
}
|
}
|
||||||
if (nud && nud->memlimit && gl.loglua) {
|
if (nud && nud->memlimit && gl.loglua) {
|
||||||
lua_gc(L, LUA_GCCOLLECT);
|
|
||||||
livelog_printf(LL_DEBUG, "LUASTATS PMEM %d:%s %lu", nud->sid,
|
livelog_printf(LL_DEBUG, "LUASTATS PMEM %d:%s %lu", nud->sid,
|
||||||
nud->name, (long unsigned) nhl_getmeminuse(L));
|
nud->name, (long unsigned) nhl_getmeminuse(L));
|
||||||
}
|
}
|
||||||
@@ -1958,10 +1941,10 @@ nhl_pcall_handle(lua_State *L, int nargs, int nresults, const char *name,
|
|||||||
impossible("Lua error: %d:%s %s", nud->sid,
|
impossible("Lua error: %d:%s %s", nud->sid,
|
||||||
nud->name ? nud->name : "(unknown)",
|
nud->name ? nud->name : "(unknown)",
|
||||||
lua_tostring(L, -1));
|
lua_tostring(L, -1));
|
||||||
|
/* Drop the error. If the caller cares, use nhl_pcall(). */
|
||||||
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (L == gl.luacore)
|
|
||||||
lua_gc(L, LUA_GCCOLLECT);
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2160,7 +2143,6 @@ nhl_done(lua_State *L)
|
|||||||
nud->name, ic);
|
nud->name, ic);
|
||||||
}
|
}
|
||||||
if (nud && nud->memlimit && !nud->perpcall) {
|
if (nud && nud->memlimit && !nud->perpcall) {
|
||||||
lua_gc(L, LUA_GCCOLLECT);
|
|
||||||
livelog_printf(LL_DEBUG, "LUASTATS DMEM %d:%s %lu", nud->sid,
|
livelog_printf(LL_DEBUG, "LUASTATS DMEM %d:%s %lu", nud->sid,
|
||||||
nud->name, (long unsigned) nhl_getmeminuse(L));
|
nud->name, (long unsigned) nhl_getmeminuse(L));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user