From c607915ef83e58882a1f8d10fd89891dce3251d9 Mon Sep 17 00:00:00 2001
From: Twinaphex <libretro@gmail.com>
Date: Thu, 11 Jun 2015 21:12:08 +0200
Subject: [PATCH] (Menu) Cleanups

---
 menu/drivers/glui.c |  1 +
 menu/drivers/xmb.c  | 19 +++++++++++++++----
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/menu/drivers/glui.c b/menu/drivers/glui.c
index 540962b7e2..d608748abe 100644
--- a/menu/drivers/glui.c
+++ b/menu/drivers/glui.c
@@ -527,6 +527,7 @@ static void glui_free(void *data)
 
    if (menu->userdata)
       free(menu->userdata);
+   menu->userdata = NULL;
 }
 
 static void glui_context_bg_destroy(glui_handle_t *glui)
diff --git a/menu/drivers/xmb.c b/menu/drivers/xmb.c
index 3a13f55595..b5d6c84b39 100644
--- a/menu/drivers/xmb.c
+++ b/menu/drivers/xmb.c
@@ -1552,14 +1552,18 @@ static void *xmb_init(void)
 error:
    if (menu)
       free(menu);
+
    if (xmb)
    {
       if (xmb->menu_stack_old)
          free(xmb->menu_stack_old);
+      xmb->menu_stack_old = NULL;
       if (xmb->selection_buf_old)
          free(xmb->selection_buf_old);
+      xmb->selection_buf_old = NULL;
       if (xmb->horizontal_list)
          free(xmb->horizontal_list);
+      xmb->horizontal_list = NULL;
    }
    return NULL;
 }
@@ -1579,13 +1583,20 @@ static void xmb_free(void *data)
       if (!xmb)
          return;
 
-      free(xmb->menu_stack_old);
-      free(xmb->selection_buf_old);
-      free(xmb->horizontal_list);
+      if (xmb->menu_stack_old)
+         free(xmb->menu_stack_old);
+      xmb->menu_stack_old = NULL;
+      if (xmb->selection_buf_old)
+         free(xmb->selection_buf_old);
+      xmb->selection_buf_old = NULL;
+      if (xmb->horizontal_list)
+         free(xmb->horizontal_list);
+      xmb->horizontal_list = NULL;
 
       gl_coord_array_free(&xmb->raster_block.carr);
 
-      free(menu->userdata);
+      if (menu->userdata)
+         free(menu->userdata);
       menu->userdata = NULL;
    }