From 4140a0023c17e06da7886a211040d7381fdb7452 Mon Sep 17 00:00:00 2001 From: PatR Date: Thu, 7 Dec 2023 18:36:14 -0800 Subject: [PATCH] fix #4059 - TTY_PERM_INVENT not freeing memory As part of the tty resize handling revision, code dealing with the perm_invent window was moved out of tty_destroy_nhwindow() was moved into a separate routine. The new routine would have been called for a window of NHW_PERMINVENT, but WIN_INVENT doesn't have that type, just ordinary NHW_MENU, so the cleanup wasn't happening, resulting in a memory leak. --- src/end.c | 4 ++-- win/tty/wintty.c | 28 +++++++++++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/end.c b/src/end.c index 416e77d47..4ac2124f1 100644 --- a/src/end.c +++ b/src/end.c @@ -1,4 +1,4 @@ -/* NetHack 3.7 end.c $NHDT-Date: 1700012887 2023/11/15 01:48:07 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.282 $ */ +/* NetHack 3.7 end.c $NHDT-Date: 1702002966 2023/12/08 02:36:06 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.284 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Robert Patrick Rankin, 2012. */ /* NetHack may be freely redistributed. See license for details. */ @@ -1344,7 +1344,7 @@ done_object_cleanup(void) a normal popup one; avoids "Bad fruit #n" when saving bones */ if (iflags.perm_invent) { iflags.perm_invent = FALSE; - update_inventory(); /* make interface notice the change */ + perm_invent_toggled(TRUE); /* make interface notice the change */ } return; } diff --git a/win/tty/wintty.c b/win/tty/wintty.c index 8276f3a09..fb6942ae8 100644 --- a/win/tty/wintty.c +++ b/win/tty/wintty.c @@ -1,4 +1,4 @@ -/* NetHack 3.7 wintty.c $NHDT-Date: 1701723543 2023/12/04 20:59:03 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.369 $ */ +/* NetHack 3.7 wintty.c $NHDT-Date: 1702002970 2023/12/08 02:36:10 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.376 $ */ /* Copyright (c) David Cohrs, 1991 */ /* NetHack may be freely redistributed. See license for details. */ @@ -272,7 +272,7 @@ static long last_glyph_reset_when; #endif static boolean calling_from_update_inventory = FALSE; static int ttyinv_create_window(int, struct WinDesc *); -static void ttyinv_remove_data(boolean); +static void ttyinv_remove_data(struct WinDesc *, boolean); static void ttyinv_add_menu(winid, struct WinDesc *, char ch, int attr, int clr, const char *str); static int selector_to_slot(char ch, const int invflags, boolean *ignore); @@ -1087,17 +1087,20 @@ tty_clear_nhwindow(winid window) break; case NHW_MENU: case NHW_TEXT: +#ifdef TTY_PERM_INVENT + case NHW_PERMINVENT: +#endif if (!erasing_tty_screen) { if (cw->active) erase_menu_or_text(window, cw, TRUE); - free_window_info(cw, FALSE); +#ifdef TTY_PERM_INVENT + if (window == WIN_INVEN) + ttyinv_remove_data(cw, FALSE); + else +#endif + free_window_info(cw, FALSE); } break; -#ifdef TTY_PERM_INVENT - case NHW_PERMINVENT: - ttyinv_remove_data(FALSE); - break; -#endif } cw->curx = cw->cury = 0; /* cw->blanked = TRUE; -- this isn't used */ @@ -1942,6 +1945,7 @@ tty_dismiss_nhwindow(winid window) break; case NHW_MENU: case NHW_TEXT: + case NHW_PERMINVENT: if (cw->active) { /* skip erasure if window_inited has been reset to 0 during final run-down in case this is the end-of-game window; @@ -1985,8 +1989,8 @@ tty_destroy_nhwindow(winid window) if (cw->type == NHW_MAP) term_clear_screen(); #ifdef TTY_PERM_INVENT - if (cw->type == NHW_PERMINVENT) - ttyinv_remove_data(TRUE); + if (cw->type == NHW_PERMINVENT || window == WIN_INVEN) + ttyinv_remove_data(cw, TRUE); #endif free_window_info(cw, TRUE); free((genericptr_t) cw); @@ -2944,10 +2948,8 @@ ttyinv_create_window(int newid, struct WinDesc *newwin) /* discard perminvent window or erase it and set remembered data to spaces */ static void -ttyinv_remove_data(boolean destroy) +ttyinv_remove_data(struct WinDesc *cw, boolean destroy) { - struct WinDesc *cw = (WIN_INVEN != WIN_ERR) ? wins[WIN_INVEN] : NULL; - if (!cw) { impossible("Removing ttyinv data for nonexistent perm invent window?"); return;