From f49ac037f9e726a79e2de0f53a5843593b77445f Mon Sep 17 00:00:00 2001 From: PatR Date: Wed, 25 Mar 2026 15:54:44 -0700 Subject: [PATCH] release wish history memory The memory leak only happened at end of game so wasn't a big deal. --- include/extern.h | 1 + src/save.c | 1 + src/zap.c | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/extern.h b/include/extern.h index ac0992b05..a3cc23213 100644 --- a/include/extern.h +++ b/include/extern.h @@ -3989,6 +3989,7 @@ extern char *item_what(int); extern int destroy_items(struct monst *, int, int) NONNULLARG1; extern int resist(struct monst *, char, int, int) NONNULLARG1; extern void wish_history_add(char *); +extern void wish_history_flush(void); extern void makewish(void); extern const char *flash_str(int, boolean) NONNULL; diff --git a/src/save.c b/src/save.c index 461ed10a1..a7ea05e7a 100644 --- a/src/save.c +++ b/src/save.c @@ -1111,6 +1111,7 @@ freedynamicdata(void) cmdq_clear(CQ_REPEAT); cmdbind_freeall(); free_tutorial(); /* (only needed if quitting while in tutorial) */ + wish_history_flush(); /* per-turn data, but might get added to when freeing other stuff */ dobjsfree(); /* really free deleted objects */ diff --git a/src/zap.c b/src/zap.c index a1466e1d5..83f25195a 100644 --- a/src/zap.c +++ b/src/zap.c @@ -6240,13 +6240,28 @@ wish_history_add(char *buf) if (wish_history[idx]) free(wish_history[idx]); - wish_history[idx] = (char *)alloc(strlen(buf) + 1); + wish_history[idx] = (char *) alloc(strlen(buf) + 1); strcpy(wish_history[idx], buf); wish_history_idx = (wish_history_idx + 1) % MAX_WISH_HISTORY; } #endif /* DEBUG */ } +/* release any old wish text; called from freedynamicdata(save.c) */ +void +wish_history_flush(void) +{ +#ifdef DEBUG + int idx; + + for (idx = 0; idx < MAX_WISH_HISTORY; ++idx) { + if (wish_history[idx]) + free((genericptr_t) wish_history[idx]), wish_history[idx] = NULL; + } + wish_history_idx = 0; +#endif +} + /* shows menu of previous wishes, copies selected into buf, max BUFSZ len. buf is not modified, if nothing was selected. */ staticfn void