home · contact · privacy
At clean-up, free memory of map object definitions, too.
[plomrogue] / src / rexit.c
index 223c79ba0a2c84630bebe07739e8234a15d6ccbd..e0d029f124b1d6218c2dde85e8b3167cd371aa12 100644 (file)
@@ -2,16 +2,21 @@
 
 #include "rexit.h"
 #include <stdlib.h> /* for exit(), free(), defines EXIT_SUCESS, EXIT_FAILURE */
-#include <stdio.h> /* for printf() */
+#include <stdio.h> /* for printf(), perror() */
+#include <stdint.h> /* for uint8_t */
 #include <ncurses.h> /* for endwin() */
+#include <errno.h> /* for errno */
 #include "main.h" /* for World struct */
 #include "map.h" /* for Map struct */
 #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() */
 
 
 
 /* The clean-up routine and the flag resource by which it decides what to do. */
-static unsigned char cleanup_flags = 0x00;
+static uint32_t cleanup_flags = 0x0000;
 static void cleanup(struct World * world);
 
 
@@ -40,6 +45,31 @@ static void cleanup(struct World * world)
     {
         free(world->log);
     }
+    if (cleanup_flags & CLEANUP_COMMAND_DB)
+    {
+        free_command_db(world);
+    }
+    if (cleanup_flags & CLEANUP_WIN_INFO)
+    {
+        free_win(world->wins.info);
+    }
+    if (cleanup_flags & CLEANUP_WIN_MAP)
+    {
+        free_win(world->wins.map);
+    }
+    if (cleanup_flags & CLEANUP_WIN_LOG)
+    {
+        free_win(world->wins.log);
+    }
+    if (cleanup_flags & CLEANUP_WIN_KEYS)
+    {
+        free_win(world->wins.keys);
+    }
+    if (cleanup_flags & CLEANUP_MAP_OBJECT_DEFS)
+    {
+        free_item_defs(world->item_def);
+        free_monster_defs(world->monster_def);
+    }
 }
 
 
@@ -59,13 +89,22 @@ extern void exit_game(struct World * world)
 
 
 
-extern void exit_err(unsigned char fail, struct World * world, char * msg)
+extern void exit_err(uint8_t err, struct World * world, char * msg)
 {
-    if (0 == fail)
+    if (0 == err)
     {
         return;
     }
     cleanup(world);
-    printf(msg);
+    if (NULL == msg)
+    {
+        msg = "Details unknown.";
+    }
+    printf("Aborted PlomRogue due to error. %s\nInternal error code: %d\n",
+           msg, err);
+    if (0 != errno)
+    {
+        perror("errno states");
+    }
     exit(EXIT_FAILURE);
 }