memory tracking fix

'heaputil' is producing a lot of complaints.  This fixes one of them,
about freeing memory that was never allocated.  In this case, it's
when removing an overview annotation for a level.  The annotation
is using dupstr_n() and not being recorded due to dupstr_n() being
placed after MONITOR_HEAP undefines the macro that overrides alloc().

There's only one use of dupstr_n(), and its length checking isn't
needed there, so just switch to dupstr() and comment out the
implementation of dupstr_n().  I left the prototype in extern.h;
that's harmless.

If dupstr_n() needs to be resurrected, a second MONITOR_HEAP-aware
version should be implemented, with corresponding macro to choose
which one to use.
This commit is contained in:
PatR
2025-04-17 10:06:46 -07:00
parent 178bd3a988
commit f1ba320cb3
2 changed files with 11 additions and 10 deletions
+3 -1
View File
@@ -246,6 +246,8 @@ dupstr(const char *string)
return strcpy((char *) alloc(len + 1), string); return strcpy((char *) alloc(len + 1), string);
} }
#if 0 /* suppress this; if included, it will need a MONITOR_HEAP edition */
/* similar for reasonable size strings, but return length of input as well */ /* similar for reasonable size strings, but return length of input as well */
char * char *
dupstr_n(const char *string, unsigned int *lenout) dupstr_n(const char *string, unsigned int *lenout)
@@ -257,7 +259,7 @@ dupstr_n(const char *string, unsigned int *lenout)
*lenout = (unsigned int) len; *lenout = (unsigned int) len;
return strcpy((char *) alloc(len + 1), string); return strcpy((char *) alloc(len + 1), string);
} }
#endif
/* cast to int or panic on overflow; use via macro */ /* cast to int or panic on overflow; use via macro */
int int
+8 -9
View File
@@ -2554,7 +2554,9 @@ query_annotation(d_level *lev)
/* add new annotation, unless it's all spaces (which will be an /* add new annotation, unless it's all spaces (which will be an
empty string after mungspaces() above) */ empty string after mungspaces() above) */
if (*nbuf && strcmp(nbuf, " ")) { if (*nbuf && strcmp(nbuf, " ")) {
mptr->custom = dupstr_n(nbuf,&mptr->custom_lth); mptr->custom = dupstr(nbuf);
/* _lth field does not include trailing '\0' in the count */
mptr->custom_lth = (unsigned) strlen(mptr->custom);
} }
} }
@@ -2668,27 +2670,24 @@ rm_mapseen(int ledger_num)
if (svd.dungeons[mptr->lev.dnum].ledger_start + mptr->lev.dlevel if (svd.dungeons[mptr->lev.dnum].ledger_start + mptr->lev.dlevel
== ledger_num) == ledger_num)
break; break;
if (!mptr) if (!mptr)
return; return;
if (mptr->custom) if (mptr->custom)
free((genericptr_t) mptr->custom); free((genericptr_t) mptr->custom), mptr->custom = (char *) NULL;
bp = mptr->final_resting_place; bpnext = mptr->final_resting_place;
while (bp) { while ((bp = bpnext) != NULL) {
bpnext = bp->next; bpnext = bp->next;
free(bp); free((genericptr_t) bp);
bp = bpnext;
} }
if (mprev) { if (mprev) {
mprev->next = mptr->next; mprev->next = mptr->next;
free(mptr);
} else { } else {
svm.mapseenchn = mptr->next; svm.mapseenchn = mptr->next;
free(mptr);
} }
free(mptr);
} }
staticfn void staticfn void