From d6643b0a516f94d914121ac21db6276e6277905d Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 24 Jul 2013 16:10:13 +0100
Subject: [PATCH] [Fix] More warnings.

---
 src/menu.c    | 24 ++++++++++++++++++++++--
 src/opengl.c  | 12 ++++++++++--
 src/toolkit.c |  2 ++
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/src/menu.c b/src/menu.c
index e0ad89e..9c8f940 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -48,6 +48,7 @@ int menu_open = 0;
 void menu_main_close(void);
 static void menu_main_load(char* str);
 static void menu_main_new(char* str);
+static void menu_main_exit(char* str);
 /* Small menu. */
 static void menu_small_close(char* str);
 static void edit_options(char* str);
@@ -100,7 +101,7 @@ void menu_main(void) {
                    BUTTON_WIDTH, BUTTON_HEIGHT,
                    "btnOptions", "Options", (void(*)(char*)) edit_options);
   window_addButton(wid, 20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
-                   "btnExit", "Exit", (void(*)(char*)) exit_game);
+                   "btnExit", "Exit", menu_main_exit);
 
   menu_Open(MENU_MAIN);
 }
@@ -108,7 +109,7 @@ void menu_main(void) {
 void menu_main_close(void) {
   window_destroy(window_get("Main Menu"));
 
-  gl_freeTexture(window_getImage(window_get("BG"), "imgBG"));
+  gl_freeTexture(window_getImage(window_get("BG"), "imgLogo"));
   window_destroy(window_get("BG"));
 
   menu_Close(MENU_MAIN);
@@ -127,6 +128,25 @@ static void menu_main_new(char* str) {
   player_new();
 }
 
+static void menu_main_exit(char* str) {
+  (void)str;
+  unsigned int wid;
+
+  wid = window_get("BG");
+
+  /*
+   * Ugly hack to prevent player.c from segfaulting due to the fact
+   * that game will attempt to render while waiting for the quit event
+   * pushed by exit_game() to be handled without actually having a player
+   * nor anything of the likes (nor toolkit to stop rendering) while not
+   * leaking any texture.
+   */
+  gl_freeTexture(window_getImage(wid, "imgLogo"));
+  window_modifyImage(wid, "imgLogo", NULL);
+
+  exit_game();
+}
+
 /* Ze ingame menu. */
 /* Small ingame menu. */
 void menu_small(void) {
diff --git a/src/opengl.c b/src/opengl.c
index ee80c13..abc5222 100644
--- a/src/opengl.c
+++ b/src/opengl.c
@@ -453,6 +453,11 @@ glTexture* gl_newSprite(const char* path, const int sx, const int sy) {
 void gl_freeTexture(glTexture* texture) {
   glTexList* cur, *last;
 
+  if(texture == NULL) {
+    WARN("Attempting to free NULL texture!");
+    return;
+  }
+
   /* See if we can find it in stack. */
   last = NULL;
   for(cur = texture_list; cur != NULL; cur = cur->next) {
@@ -466,8 +471,11 @@ void gl_freeTexture(glTexture* texture) {
         free(texture);
 
         /* Free the list node. */
-        if(last == NULL) /* Incase it's the last texture. */
-          texture_list = NULL;
+        if(last == NULL) /* Case there's no texture before it. */
+          if(cur->next != NULL)
+            texture_list = cur->next;
+          else /* Case it's the last texture. */
+            texture_list = NULL;
         else
           last->next = cur->next;
         free(cur);
diff --git a/src/toolkit.c b/src/toolkit.c
index 01c600d..b3cce92 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -377,6 +377,8 @@ static Widget* window_getwgt(const unsigned int wid, char* name) {
   for(i = 0; i < wdw->nwidgets; i++)
     if(strcmp(wdw->widgets[i].name, name)==0)
       return &wdw->widgets[i];
+
+  WARN("Widget '%s' not found in window '%u'!", name, wid);
   return NULL;
 }