gamestate_cleanup() is now safe to be called multiple times

Tue, 28 Jul 2026 13:37:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2026 13:37:19 +0200
changeset 158
52d452d0e7bf
parent 157
07cbfc477b22
child 159
07c7be5661f4

gamestate_cleanup() is now safe to be called multiple times

fixes #950

src/chess/rules.c file | annotate | diff | comparison | revisions
--- a/src/chess/rules.c	Tue Jul 28 12:58:49 2026 +0200
+++ b/src/chess/rules.c	Tue Jul 28 13:37:19 2026 +0200
@@ -62,12 +62,19 @@
 }
 
 void gamestate_cleanup(GameState *gamestate) {
+    if (gamestate == NULL) return;
+
     free(gamestate->moves);
+    gamestate->moves = NULL;
     free(gamestate->fen_start);
-    for (unsigned i = 0 ; i < gamestate->movecount ; i++) {
-        free(gamestate->fen[i]);
+    gamestate->fen_start = NULL;
+    if (gamestate->fen) {
+        for (unsigned i = 0 ; i < gamestate->movecount ; i++) {
+            free(gamestate->fen[i]);
+        }
+        free(gamestate->fen);
+        gamestate->fen = NULL;
     }
-    free(gamestate->fen);
     gamestate->movecount = gamestate->movecapacity = 0;
 }
 

mercurial