plug a couple of memory leaks in sp_lev.c, take II

[...]
| Change selection_free(foo) to also free(foo) after freeing foo's
| fields.  Every use was already
|   selection_free(foo);
|   free(foo);
| except for the two instances of memory leak.

And except for the three which aren't in sp_lev.c, one of which was
dealing with memory managed by Lua.  This time it seems to be working
as intended.
This commit is contained in:
PatR
2020-02-12 18:56:41 -08:00
parent 914c78546f
commit 75e9055b89
4 changed files with 41 additions and 42 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
/* NetHack 3.6 extern.h $NHDT-Date: 1581322657 2020/02/10 08:17:37 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.795 $ */ /* NetHack 3.6 extern.h $NHDT-Date: 1581562570 2020/02/13 02:56:10 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.796 $ */
/* Copyright (c) Steve Creps, 1988. */ /* Copyright (c) Steve Creps, 1988. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -2471,7 +2471,7 @@ E void FDECL(fill_room, (struct mkroom *, BOOLEAN_P));
E boolean FDECL(load_special, (const char *)); E boolean FDECL(load_special, (const char *));
E xchar FDECL(selection_getpoint, (int, int, struct selectionvar *)); E xchar FDECL(selection_getpoint, (int, int, struct selectionvar *));
E struct selectionvar *NDECL(selection_new); E struct selectionvar *NDECL(selection_new);
E void FDECL(selection_free, (struct selectionvar *)); E void FDECL(selection_free, (struct selectionvar *, BOOLEAN_P));
#if !defined(IN_SP_LEV_C) #if !defined(IN_SP_LEV_C)
E void FDECL(set_selection_floodfillchk, (int FDECL((*), (int,int)))); E void FDECL(set_selection_floodfillchk, (int FDECL((*), (int,int))));
#endif #endif
+2 -2
View File
@@ -1,4 +1,4 @@
/* NetHack 3.6 do_name.c $NHDT-Date: 1578764034 2020/01/11 17:33:54 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.169 $ */ /* NetHack 3.6 do_name.c $NHDT-Date: 1581562587 2020/02/13 02:56:27 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.172 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Pasi Kallinen, 2018. */ /*-Copyright (c) Pasi Kallinen, 2018. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -326,7 +326,7 @@ void
gloc_filter_done() gloc_filter_done()
{ {
if (g.gloc_filter_map) { if (g.gloc_filter_map) {
selection_free(g.gloc_filter_map); selection_free(g.gloc_filter_map, TRUE);
g.gloc_filter_map = (struct selectionvar *) 0; g.gloc_filter_map = (struct selectionvar *) 0;
} }
+8 -6
View File
@@ -1,4 +1,4 @@
/* NetHack 3.7 nhlua.c $NHDT-Date: 1581280068 2020/02/09 20:27:48 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.8 $ */ /* NetHack 3.7 nhlua.c $NHDT-Date: 1581562591 2020/02/13 02:56:31 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.9 $ */
/* Copyright (c) 2018 by Pasi Kallinen */ /* Copyright (c) 2018 by Pasi Kallinen */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -54,7 +54,7 @@ int index;
struct selectionvar *sel; struct selectionvar *sel;
luaL_checktype(L, index, LUA_TUSERDATA); luaL_checktype(L, index, LUA_TUSERDATA);
sel = (struct selectionvar *)luaL_checkudata(L, index, "selection"); sel = (struct selectionvar *) luaL_checkudata(L, index, "selection");
if (!sel) if (!sel)
nhl_error(L, "Selection error"); nhl_error(L, "Selection error");
return sel; return sel;
@@ -65,8 +65,9 @@ l_selection_gc(L)
lua_State *L; lua_State *L;
{ {
struct selectionvar *sel = l_selection_check(L, 1); struct selectionvar *sel = l_selection_check(L, 1);
if (sel) if (sel)
selection_free(sel); selection_free(sel, FALSE);
return 0; return 0;
} }
@@ -89,15 +90,16 @@ l_selection_push(L)
lua_State *L; lua_State *L;
{ {
struct selectionvar *tmp = selection_new(); struct selectionvar *tmp = selection_new();
struct selectionvar *sel = (struct selectionvar *)lua_newuserdata(L, sizeof(struct selectionvar)); struct selectionvar
*sel = (struct selectionvar *) lua_newuserdata(L, sizeof sel);
luaL_getmetatable(L, "selection"); luaL_getmetatable(L, "selection");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
sel->wid = tmp->wid; sel->wid = tmp->wid;
sel->hei = tmp->hei; sel->hei = tmp->hei;
sel->map = dupstr(tmp->map); sel->map = dupstr(tmp->map);
selection_free(tmp); selection_free(tmp, TRUE);
free(tmp);
return sel; return sel;
} }
+29 -32
View File
@@ -1,4 +1,4 @@
/* NetHack 3.6 sp_lev.c $NHDT-Date: 1580610435 2020/02/02 02:27:15 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.153 $ */ /* NetHack 3.6 sp_lev.c $NHDT-Date: 1581562593 2020/02/13 02:56:33 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.157 $ */
/* Copyright (c) 1989 by Jean-Christophe Collet */ /* Copyright (c) 1989 by Jean-Christophe Collet */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -3581,12 +3581,11 @@ lua_State *L UNUSED;
struct selectionvar * struct selectionvar *
selection_new() selection_new()
{ {
struct selectionvar * struct selectionvar *tmps = (struct selectionvar *) alloc(sizeof tmps);
tmps = (struct selectionvar *) alloc(sizeof (struct selectionvar));
tmps->wid = COLNO; tmps->wid = COLNO;
tmps->hei = ROWNO; tmps->hei = ROWNO;
tmps->map = (char *)alloc((COLNO * ROWNO) + 1); tmps->map = (char *) alloc((COLNO * ROWNO) + 1);
(void) memset(tmps->map, 1, (COLNO * ROWNO)); (void) memset(tmps->map, 1, (COLNO * ROWNO));
tmps->map[(COLNO * ROWNO)] = '\0'; tmps->map[(COLNO * ROWNO)] = '\0';
@@ -3594,14 +3593,18 @@ selection_new()
} }
void void
selection_free(sel) selection_free(sel, freesel)
struct selectionvar *sel; struct selectionvar *sel;
boolean freesel;
{ {
if (!sel) if (sel) {
return; Free(sel->map);
Free(sel->map); sel->map = NULL;
sel->map = NULL; if (freesel)
sel->wid = sel->hei = 0; free((genericptr_t) sel);
else
sel->wid = sel->hei = 0;
}
} }
struct selectionvar * struct selectionvar *
@@ -3811,8 +3814,7 @@ int dir;
if (selection_getpoint(x, y, tmp)) if (selection_getpoint(x, y, tmp))
selection_setpoint(x, y, ov, 1); selection_setpoint(x, y, ov, 1);
selection_free(tmp); selection_free(tmp, TRUE);
free(tmp);
} }
static int FDECL((*selection_flood_check_func), (int, int)); static int FDECL((*selection_flood_check_func), (int, int));
@@ -3897,7 +3899,7 @@ boolean diagonals;
xchar dy[SEL_FLOOD_STACK]; xchar dy[SEL_FLOOD_STACK];
if (selection_flood_check_func == (int FDECL((*), (int, int))) 0) { if (selection_flood_check_func == (int FDECL((*), (int, int))) 0) {
selection_free(tmp); selection_free(tmp, TRUE);
return; return;
} }
SEL_FLOOD(x, y); SEL_FLOOD(x, y);
@@ -3923,8 +3925,7 @@ boolean diagonals;
#undef SEL_FLOOD #undef SEL_FLOOD
#undef SEL_FLOOD_STACK #undef SEL_FLOOD_STACK
#undef SEL_FLOOD_CHKDIR #undef SEL_FLOOD_CHKDIR
selection_free(tmp); selection_free(tmp, TRUE);
free(tmp);
} }
/* McIlroy's Ellipse Algorithm */ /* McIlroy's Ellipse Algorithm */
@@ -4554,7 +4555,7 @@ struct selectionvar *ov;
/* try to make a hole or a trapdoor */ /* try to make a hole or a trapdoor */
if (Can_fall_thru(&u.uz)) { if (Can_fall_thru(&u.uz)) {
selection_free(ov3); selection_free(ov3, TRUE);
ov3 = selection_clone(ov2); ov3 = selection_clone(ov2);
while (selection_rndcoord(ov3, &x, &y, TRUE)) { while (selection_rndcoord(ov3, &x, &y, TRUE)) {
if (maketrap(x,y, rn2(2) ? HOLE : TRAPDOOR)) if (maketrap(x,y, rn2(2) ? HOLE : TRAPDOOR))
@@ -4570,10 +4571,8 @@ struct selectionvar *ov;
res = FALSE; res = FALSE;
gotitdone: gotitdone:
selection_free(ov2); selection_free(ov2, TRUE);
free(ov2); selection_free(ov3, TRUE);
selection_free(ov3);
free(ov3);
return res; return res;
} }
@@ -4606,19 +4605,19 @@ ensure_way_out()
do { do {
ret = TRUE; ret = TRUE;
for (x = 0; x < COLNO; x++) for (x = 1; x < COLNO; x++)
for (y = 0; y < ROWNO; y++) for (y = 0; y < ROWNO; y++)
if (ACCESSIBLE(levl[x][y].typ) if (ACCESSIBLE(levl[x][y].typ)
&& !selection_getpoint(x, y, ov)) { && !selection_getpoint(x, y, ov)) {
if (generate_way_out_method(x,y, ov)) if (generate_way_out_method(x, y, ov))
selection_floodfill(ov, x,y, TRUE); selection_floodfill(ov, x, y, TRUE);
ret = FALSE; ret = FALSE;
goto outhere; goto outhere;
} }
outhere: ; outhere:
;
} while (!ret); } while (!ret);
selection_free(ov); selection_free(ov, TRUE);
free(ov);
} }
int int
@@ -5181,9 +5180,9 @@ int prop;
create_des_coder(); create_des_coder();
if (argc == 1) if (argc == 1) {
sel = l_selection_check(L, -1); sel = l_selection_check(L, -1);
else if (argc == 0) { } else if (argc == 0) {
freesel = TRUE; freesel = TRUE;
sel = selection_new(); sel = selection_new();
selection_not(sel); selection_not(sel);
@@ -5191,10 +5190,8 @@ int prop;
if (sel) { if (sel) {
selection_iterate(sel, sel_set_wall_property, (genericptr_t) &prop); selection_iterate(sel, sel_set_wall_property, (genericptr_t) &prop);
if (freesel) { if (freesel)
selection_free(sel); selection_free(sel, TRUE);
free(sel);
}
} }
} }