From 1d734e789868ec275aca64240a3a43402b0b1cb0 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 24 Jul 2013 15:10:24 +0100
Subject: [PATCH] [Fix] More memory related fixes.

---
 src/opengl.c  | 22 ++++++++++++----------
 src/spfx.c    |  4 ++++
 src/toolkit.c |  8 +++-----
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/src/opengl.c b/src/opengl.c
index 455b5a6..ee80c13 100644
--- a/src/opengl.c
+++ b/src/opengl.c
@@ -345,7 +345,8 @@ glTexture* gl_loadImage(SDL_Surface* surface) {
   texture->sw = texture->w;
   texture->sh = texture->h;
 
-  texture->trans = NULL;
+  texture->trans  = NULL;
+  texture->name   = NULL;
 
   return texture;
 }
@@ -365,19 +366,20 @@ glTexture* gl_newImage(const char* path) {
     }
   }
 
-  /* Allocate memory. */
-  if(texture_list == NULL) { /* Special condition - creating new list. */
-    texture_list = cur = malloc(sizeof(glTexList));
-  } else {
-    cur = malloc(sizeof(glTexList));
-    last->next = cur;
-  }
 
-  /* Set node properties */
+  /* Create the new node. */
+  cur = malloc(sizeof(glTexList));
   cur->next = NULL;
-  cur->tex = gl_loadNewImage(path);
   cur->used = 1;
 
+  /* Load the image. */
+  cur->tex = gl_loadNewImage(path);
+
+  if(texture_list == NULL) /* Special condition - creating new list. */
+    texture_list = cur;
+  else
+    last->next = cur;
+
   return cur->tex;
 }
 
diff --git a/src/spfx.c b/src/spfx.c
index 7389abc..5cc3be9 100644
--- a/src/spfx.c
+++ b/src/spfx.c
@@ -276,9 +276,12 @@ void spfx_render(const int layer) {
     spfx_nstack = spfx_nstack_back;
     break;
   }
+
+  /* Now render the layer. */
   for(i = 0; i < spfx_nstack; i++) {
     effect = &spfx_effects[spfx_stack[i].effect];
 
+    /* Simplifies. */
     sx = (int)effect->gfx->sx;
     sy = (int)effect->gfx->sy;
 
@@ -286,6 +289,7 @@ void spfx_render(const int layer) {
       spfx_stack[i].lastframe = sx * sy
         * MIN(((double)(spfx_stack[i].timer)/(double)effect->anim), 1.);
 
+    /* Render. */
     gl_blitSprite(effect->gfx,
                   VX(spfx_stack[i].pos), VY(spfx_stack[i].pos),
                   spfx_stack[i].lastframe % sx,
diff --git a/src/toolkit.c b/src/toolkit.c
index c699db9..01c600d 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -1670,11 +1670,9 @@ int toolkit_init(void) {
 
 /* Exit the toolkit. */
 void toolkit_exit(void) {
-  int i;
-  for(i = 0; i < nwindows; i++) {
-    window_destroy(windows[i].id);
-    free(windows);
-  }
+  while(nwindows > 0)
+    window_destroy(windows[0].id);
+  free(windows);
 }
 
 /* Spawns a secondary loop that only works until the toolkit dies. */