From c1a7e6cdb13cd7d883424afdf0fe08e9a10fbc28 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sat, 31 Aug 2013 01:48:01 +0200 Subject: [PATCH] At clean-up, free memory of map object definitions, too. --- src/main.c | 2 ++ src/map_objects.c | 25 +++++++++++++++++++++++++ src/map_objects.h | 8 ++++++++ src/rexit.c | 6 ++++++ src/rexit.h | 19 ++++++++++--------- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 0ecc10a..9b75209 100644 --- a/src/main.c +++ b/src/main.c @@ -103,6 +103,8 @@ int main(int argc, char *argv[]) world.monster = 0; world.item = 0; init_map_object_defs(&world, "config/defs"); + set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS); + exit_err(1, &world, NULL); /* For interactive mode, try to load world state from savefile. */ char * err_o = "Trouble loading game (fopen() in main()) / " diff --git a/src/map_objects.c b/src/map_objects.c index e35a88c..4cd6cb0 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -139,6 +139,31 @@ extern void init_map_object_defs(struct World * world, char * filename) +extern void free_item_defs(struct ItemDef * id_start) +{ + if (0 != id_start->map_obj_def.next) + { + free_item_defs((struct ItemDef *) id_start->map_obj_def.next); + } + free(id_start->map_obj_def.desc); + free(id_start); +} + + + + +extern void free_monster_defs(struct MonsterDef * md_start) +{ + if (0 != md_start->map_obj_def.next) + { + free_monster_defs((struct MonsterDef *) md_start->map_obj_def.next); + } + free(md_start->map_obj_def.desc); + free(md_start); +} + + + extern uint8_t write_map_objects(struct World * world, void * start, FILE * file) { diff --git a/src/map_objects.h b/src/map_objects.h index 4804033..83ae7c5 100644 --- a/src/map_objects.h +++ b/src/map_objects.h @@ -79,6 +79,14 @@ extern void init_map_object_defs(struct World * world, char * filename); +/* Free item / monster definitions in map object chain starting at "md_start" / + * "id_start". + */ +extern void free_item_defs(struct ItemDef * id_start); +extern void free_monster_defs(struct MonsterDef * md_start); + + + /* Build into memory starting at "start" chain of "n" map objects of type * "def_id". */ diff --git a/src/rexit.c b/src/rexit.c index 85e2bfc..e0d029f 100644 --- a/src/rexit.c +++ b/src/rexit.c @@ -11,6 +11,7 @@ #include "keybindings.h" /* for KeysWinData, KeyBinding structs */ #include "command_db.h" /* for free_command_db() */ #include "windows.h" /* for Win struct, free_win() */ +#include "map_objects.h" /* for free_item_defs(), free_monster_defs() */ @@ -64,6 +65,11 @@ static void cleanup(struct World * world) { free_win(world->wins.keys); } + if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS) + { + free_item_defs(world->item_def); + free_monster_defs(world->monster_def); + } } diff --git a/src/rexit.h b/src/rexit.h index 3ba34c4..6336788 100644 --- a/src/rexit.h +++ b/src/rexit.h @@ -21,15 +21,16 @@ struct Map; */ enum cleanup_flag { - CLEANUP_NCURSES = 0x0001, - CLEANUP_MAP = 0x0002, - CLEANUP_KEYBINDINGS = 0x0004, - CLEANUP_LOG = 0x0008, - CLEANUP_COMMAND_DB = 0x0010, - CLEANUP_WIN_INFO = 0x0020, - CLEANUP_WIN_LOG = 0x0040, - CLEANUP_WIN_MAP = 0x0080, - CLEANUP_WIN_KEYS = 0x0100 + CLEANUP_NCURSES = 0x0001, + CLEANUP_MAP = 0x0002, + CLEANUP_KEYBINDINGS = 0x0004, + CLEANUP_LOG = 0x0008, + CLEANUP_COMMAND_DB = 0x0010, + CLEANUP_MAP_OBJECT_DEFS = 0x0020, + CLEANUP_WIN_INFO = 0x0040, + CLEANUP_WIN_LOG = 0x0080, + CLEANUP_WIN_MAP = 0x0100, + CLEANUP_WIN_KEYS = 0x0200 }; extern void set_cleanup_flag(enum cleanup_flag flag); -- 2.30.2