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]
This commit is contained in:
Simon Tatham
2010-01-04 19:14:35 +00:00
parent 56b421a632
commit 4dfd8512ab

View File

@ -156,6 +156,15 @@ midend *midend_new(frontend *fe, const game *ourgame,
return me; 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) static void midend_free_game(midend *me)
{ {
while (me->nstates > 0) { while (me->nstates > 0) {
@ -511,8 +520,7 @@ void midend_restart_game(midend *me)
* Now enter the restarted state as the next move. * Now enter the restarted state as the next move.
*/ */
midend_stop_anim(me); midend_stop_anim(me);
while (me->nstates > me->statepos) midend_purge_states(me);
me->ourgame->free_game(me->states[--me->nstates].state);
ensure(me); ensure(me);
me->states[me->nstates].state = s; me->states[me->nstates].state = s;
me->states[me->nstates].movestr = dupstr(me->desc); me->states[me->nstates].movestr = dupstr(me->desc);
@ -587,8 +595,7 @@ static int midend_really_process_key(midend *me, int x, int y, int button)
goto done; goto done;
} else if (s) { } else if (s) {
midend_stop_anim(me); midend_stop_anim(me);
while (me->nstates > me->statepos) midend_purge_states(me);
me->ourgame->free_game(me->states[--me->nstates].state);
ensure(me); ensure(me);
assert(movestr != NULL); assert(movestr != NULL);
me->states[me->nstates].state = s; me->states[me->nstates].state = s;
@ -1292,11 +1299,7 @@ char *midend_solve(midend *me)
* Now enter the solved state as the next move. * Now enter the solved state as the next move.
*/ */
midend_stop_anim(me); midend_stop_anim(me);
while (me->nstates > me->statepos) { midend_purge_states(me);
me->ourgame->free_game(me->states[--me->nstates].state);
if (me->states[me->nstates].movestr)
sfree(me->states[me->nstates].movestr);
}
ensure(me); ensure(me);
me->states[me->nstates].state = s; me->states[me->nstates].state = s;
me->states[me->nstates].movestr = movestr; me->states[me->nstates].movestr = movestr;