From e6a78faa3052eff39bcadcaf81e2e3b5249071a7 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 31 Aug 2013 03:34:13 +0200
Subject: [PATCH] Improved windows cleaning up: free win_meta, ncurses
 windows/pads.

---
 src/main.c    | 9 ++++-----
 src/rexit.c   | 5 +++++
 src/rexit.h   | 3 ++-
 src/windows.c | 8 +++++++-
 src/windows.h | 2 +-
 5 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/main.c b/src/main.c
index 786e72e..d63f587 100644
--- a/src/main.c
+++ b/src/main.c
@@ -203,11 +203,10 @@ int main(int argc, char *argv[])
     raw();
     init_keybindings(&world);
     set_cleanup_flag(CLEANUP_KEYBINDINGS);
-    struct WinMeta win_meta;
     char * err_winmem = "Trouble with init_win_meta() or draw_all_wins() in "
                         "main().";
-    exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
-    world.wins.meta = &win_meta;
+    exit_err(init_win_meta(screen, &world.wins.meta), &world, err_winmem);
+    set_cleanup_flag(CLEANUP_WIN_META);
     world.wins.keys = init_win_from_file(&world, "Keys", draw_keys_win);
     set_cleanup_flag(CLEANUP_WIN_KEYS);
     world.wins.info = init_win_from_file(&world, "Info", draw_info_win);
@@ -238,7 +237,7 @@ int main(int argc, char *argv[])
         }
         while (1)
         {
-            draw_all_wins(&win_meta);
+            draw_all_wins(world.wins.meta);
             key = getch();
             if (   EOF != action
                 && key == get_action_key(world.keybindings, "wait"))
@@ -264,7 +263,7 @@ int main(int argc, char *argv[])
         while (1)
         {
             save_game(&world);
-            draw_all_wins(&win_meta);
+            draw_all_wins(world.wins.meta);
             key = getch();
             if (0 != player.hitpoints && 0 == player_control(key, &world))
             {
diff --git a/src/rexit.c b/src/rexit.c
index 2907705..8b32d4a 100644
--- a/src/rexit.c
+++ b/src/rexit.c
@@ -65,6 +65,11 @@ static void cleanup(struct World * world)
     {
         free_win(world->wins.keys);
     }
+    if (cleanup_flags & CLEANUP_WIN_META)
+    {
+        delwin(world->wins.meta->padframe.curses_win);
+        free(world->wins.meta);
+    }
     if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS)
     {
         free_item_defs(world->item_def);
diff --git a/src/rexit.h b/src/rexit.h
index f376e5e..a6b48f9 100644
--- a/src/rexit.h
+++ b/src/rexit.h
@@ -31,7 +31,8 @@ enum cleanup_flag
     CLEANUP_WIN_INFO        = 0x0080,
     CLEANUP_WIN_LOG         = 0x0100,
     CLEANUP_WIN_MAP         = 0x0200,
-    CLEANUP_WIN_KEYS        = 0x0400
+    CLEANUP_WIN_KEYS        = 0x0400,
+    CLEANUP_WIN_META        = 0x0800
 };
 extern void set_cleanup_flag(enum cleanup_flag flag);
 
diff --git a/src/windows.c b/src/windows.c
index f75544b..df67411 100644
--- a/src/windows.c
+++ b/src/windows.c
@@ -362,8 +362,9 @@ static void shift_win_backward(struct WinMeta * wmeta)
 
 
 
-extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta)
+extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta ** wmp)
 {
+    struct WinMeta * wmeta = malloc(sizeof(struct WinMeta));
     wmeta->_screen             = screen;
     uint32_t maxy_test         = getmaxy(screen);
     uint32_t maxx_test         = getmaxx(screen);
@@ -383,6 +384,7 @@ extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta)
     }
     wmeta->padframe.curses_win = pad_test;
     wmeta->active              = 0;
+    *wmp = wmeta;
     return 0;
 }
 
@@ -440,6 +442,10 @@ extern uint8_t init_win(struct WinMeta * wmeta, struct Win ** wp, char * title,
 
 extern void free_win(struct Win * win)
 {
+    if (0 != win->frame.curses_win)
+    {
+        delwin(win->frame.curses_win);
+    }
     free(win->_title);
     free(win);
 }
diff --git a/src/windows.h b/src/windows.h
index a40835d..aedb30b 100644
--- a/src/windows.h
+++ b/src/windows.h
@@ -95,7 +95,7 @@ struct WinMeta
  * initialized 0, except for the virtual screen (height = that of the terminal
  * screen; width = 1) amd its frame sized to the size of the terminal screen.
  */
-extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta);
+extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta ** wmeta);
 
 
 
-- 
2.30.2