While hunting for a memory leak in object allocation--which I haven't found yet--I discovered one in monster movement. iter_mons_safe() allocates an array of (monst *) pointers for the monsters on the current level, loops over that array to call a function for each one, then frees the array. But if the game ends while that called function is running, execution never returns to iter_mons_safe() so it wasn't able to free the memory. Since that can happen at most once per game, it wasn't a signifcant leak. This fixes it anyway. There was a second issue: make sure that iter_mons_safe() doesn't call alloc(0) to make the temporary array for zero monsters when there aren't any on the level. That might not be able to happen for monster movement but the routine is written to be more general than just movement. alloc(0) could confuse the MONITOR_HEAP code. In C89/C90 I think malloc(0) is allowed to return NULL (don't recall for sure; maybe that was just known pre-standard behavior for some implementations). Null return would trigger a panic even without MONITOR_HEAP. Don't know about C99 and later.
32 KiB
32 KiB