shithub: puzzles

Download patch

ref: 4dfd8512ab278dd9f68475ace818b29e7e7c17af
parent: 56b421a63295dd8886f4748d1c0a32153e9ff758
author: Simon Tatham <anakin@pobox.com>
date: Mon Jan 4 14:14:35 EST 2010

Memory leak fix from Tiago Dionizio: whenever we free the midend's
collection of game states, we should also free the move strings from
which they were constructed.

[originally from svn r8805]

--- a/midend.c
+++ b/midend.c
@@ -156,6 +156,15 @@
     return me;
 }
 
+static void midend_purge_states(midend *me)
+{
+    while (me->nstates > me->statepos) {
+        me->ourgame->free_game(me->states[--me->nstates].state);
+        if (me->states[me->nstates].movestr)
+            sfree(me->states[me->nstates].movestr);
+    }
+}
+
 static void midend_free_game(midend *me)
 {
     while (me->nstates > 0) {
@@ -511,8 +520,7 @@
      * Now enter the restarted state as the next move.
      */
     midend_stop_anim(me);
-    while (me->nstates > me->statepos)
-	me->ourgame->free_game(me->states[--me->nstates].state);
+    midend_purge_states(me);
     ensure(me);
     me->states[me->nstates].state = s;
     me->states[me->nstates].movestr = dupstr(me->desc);
@@ -587,8 +595,7 @@
             goto done;
         } else if (s) {
 	    midend_stop_anim(me);
-            while (me->nstates > me->statepos)
-                me->ourgame->free_game(me->states[--me->nstates].state);
+            midend_purge_states(me);
             ensure(me);
             assert(movestr != NULL);
             me->states[me->nstates].state = s;
@@ -1292,11 +1299,7 @@
      * Now enter the solved state as the next move.
      */
     midend_stop_anim(me);
-    while (me->nstates > me->statepos) {
-	me->ourgame->free_game(me->states[--me->nstates].state);
-        if (me->states[me->nstates].movestr)
-            sfree(me->states[me->nstates].movestr);
-    }
+    midend_purge_states(me);
     ensure(me);
     me->states[me->nstates].state = s;
     me->states[me->nstates].movestr = movestr;