alpha_beta: memory leaks
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 28 May 2009 16:51:52 +0200
changeset 96 810acedf60d8
parent 95 d2a071bd1a60
child 97 f883331a1bf2
alpha_beta: memory leaks After fixing all the memory leaks in this piece code, i can finally go further to implement a better KI. committer: Markus Bröker <mbroeker@largo.homelinux.org>
alpha_beta.c
--- a/alpha_beta.c
+++ b/alpha_beta.c
@@ -74,7 +74,6 @@
     board[stack_end->data->target] = EMPTY;
 
     actual = stack_end->prev;
-    free (stack_end->data);
     free (stack_end);
 
     stack_end = actual;
@@ -227,14 +226,21 @@
         undo ();
         t++;
 
-        if (moveValue >= beta)
+        if (moveValue >= beta) {
+            free (move);
             return beta;
+        }
 
         if (moveValue > alpha) {
             alpha = moveValue;
-            if (depth == desired_depth)
+            if (depth == desired_depth) {
+                if (bestMove != NULL)
+                    free (bestMove);
                 bestMove = move;
+                continue;
+            }
         }
+        free (move);
     }
 
     return alpha;
@@ -264,6 +270,7 @@
             moveValue = max_alpha_beta (depth - 1, alpha, beta);
 
         undo ();
+        free (move);
         t++;
 
         if (moveValue <= alpha)
@@ -338,6 +345,7 @@
         }
 
         board[bestMove->target] = 'O';
+        free (bestMove);
         print ();
 
         if ((estimateFunction () * estimateFunction ()) == 2500) {