From: Christian Heller Date: Fri, 30 Aug 2013 00:18:44 +0000 (+0200) Subject: Cleaned up memory allocation by Win initialization. X-Git-Tag: tce~996 X-Git-Url: https://plomlompom.com/repos/tasks?a=commitdiff_plain;h=b705b99b02a5eeb2b76605fe9b11eb6d5411d6e3;p=plomrogue Cleaned up memory allocation by Win initialization. --- diff --git a/src/main.c b/src/main.c index 566a660..bbc4302 100644 --- a/src/main.c +++ b/src/main.c @@ -204,17 +204,13 @@ int main(int argc, char *argv[]) "main()."; exit_err(init_win_meta(screen, &win_meta), &world, err_winmem); world.wins.meta = &win_meta; - struct Win win_keys = init_win_from_file(&world, "Keys", draw_keys_win); - world.wins.keys = &win_keys; + world.wins.keys = init_win_from_file(&world, "Keys", draw_keys_win); set_cleanup_flag(CLEANUP_WIN_KEYS); - struct Win win_info = init_win_from_file(&world, "Info", draw_info_win); - world.wins.info = &win_info; + world.wins.info = init_win_from_file(&world, "Info", draw_info_win); set_cleanup_flag(CLEANUP_WIN_INFO); - struct Win win_log = init_win_from_file(&world, "Log", draw_log_win); - world.wins.log = &win_log; + world.wins.log = init_win_from_file(&world, "Log", draw_log_win); set_cleanup_flag(CLEANUP_WIN_LOG); - struct Win win_map = init_win_from_file(&world, "Map", draw_map_win); - world.wins.map = &win_map; + world.wins.map = init_win_from_file(&world, "Map", draw_map_win); set_cleanup_flag(CLEANUP_WIN_MAP); sorted_wintoggle(&world); diff --git a/src/rexit.c b/src/rexit.c index 7bd4440..2049613 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 */ +#include "wincontrol.h" /* for free_win() */ @@ -50,19 +51,19 @@ static void cleanup(struct World * world) } if (cleanup_flags & CLEANUP_WIN_INFO) { - free(world->wins.info->_title); + free_win(world->wins.info); } if (cleanup_flags & CLEANUP_WIN_MAP) { - free(world->wins.map->_title); + free_win(world->wins.map); } if (cleanup_flags & CLEANUP_WIN_LOG) { - free(world->wins.log->_title); + free_win(world->wins.log); } if (cleanup_flags & CLEANUP_WIN_KEYS) { - free(world->wins.keys->_title); + free_win(world->wins.keys); } } diff --git a/src/wincontrol.c b/src/wincontrol.c index cfa593b..715aebb 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -15,8 +15,16 @@ -extern struct Win init_win_from_file(struct World * world, char * w_name, - void (* f) (struct Win *)) +extern void free_win(struct Win * win) +{ + free(win->_title); + free(win); +} + + + +extern struct Win * init_win_from_file(struct World * world, char * w_name, + void (* f) (struct Win *)) { char * err = "Trouble in init_win_from_file() with malloc()."; char * prefix = "config/windows/"; @@ -45,8 +53,8 @@ extern struct Win init_win_from_file(struct World * world, char * w_name, exit_err(fclose(file), world, err); struct WinMeta * wmeta = world->wins.meta; - struct Win w; - init_win(wmeta, &w, title, height, width, world, f); + struct Win * w = malloc(sizeof(struct Win)); + init_win(wmeta, w, title, height, width, world, f); free(title); return w; } diff --git a/src/wincontrol.h b/src/wincontrol.h index c8dc83b..cb7d0ba 100644 --- a/src/wincontrol.h +++ b/src/wincontrol.h @@ -16,12 +16,18 @@ struct World; +/* Free allocated memory for an initialized Win struct. */ +extern void free_win(struct Win * win); + + + + /* Wrapper around init_win() that reads the desired window size and title from a * file at the path prefixing the provided win name "w_name" with * "config/windows/". "f"() is the window drawing function (Win._draw()). */ -extern struct Win init_win_from_file(struct World * world, char * w_name, - void (* f) (struct Win *)); +extern struct Win * init_win_from_file(struct World * world, char * w_name, + void (* f) (struct Win *));