From: Christian Heller Date: Fri, 30 Aug 2013 23:48:01 +0000 (+0200) Subject: At clean-up, free memory of map object definitions, too. X-Git-Tag: tce~993 X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdb.prefix%7D%7D/static/bar%20baz.html?a=commitdiff_plain;h=c1a7e6cdb13cd7d883424afdf0fe08e9a10fbc28;p=plomrogue At clean-up, free memory of map object definitions, too. --- 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);