Valgrind error message is not clear

I am programming a chess engine in C and running it through Valgrind and got this:

==20217== 672 (32 direct, 640 indirect) bytes in 1 blocks are definitely lost in loss record 39 of  98
==20217==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20217==    by 0x40143A: register_move (Chess.c:236)
==20217==    by 0x4015B6: scan_in_direction (Chess.c:256)
==20217==    by 0x402131: getBishopMoves (Chess.c:374)
==20217==    by 0x401296: getSquareMoves (Chess.c:190)
==20217==    by 0x400C94: getAllMoves (Chess.c:60)
==20217==    by 0x405C19: alphaBetaMax (MiniMax.c:260)
==20217==    by 0x405D02: alphaBetaMax (MiniMax.c:274)
==20217==    by 0x405D02: alphaBetaMax (MiniMax.c:274)
==20217==    by 0x405D02: alphaBetaMax (MiniMax.c:274)
==20217==    by 0x405ECD: getBestMove (MiniMax.c:307)
==20217==    by 0x403933: computerTurn (Chess.c:850)

      

Function register_move

:

static void register_move(game_t game, int x1, int y1, int x2, int y2, char special,
          int color) {
    move_t *tmpMove = (move_t *)malloc(sizeof(move_t));
    checkMalloc(tmpMove);
    tmpMove->x1 = x1;
    tmpMove->y1 = y1;
    tmpMove->x2 = x2;
    tmpMove->y2 = y2;
    tmpMove->s = special;
    firstMove = addToList(game, firstMove, tmpMove, color); //firstMove is a global list of moves
}

      

EDIT: For more clarity, move_t

defined as:

struct move_t {
    int x1;
    int y1;
    int x2;
    int y2;
    char s;
    struct move_t *next;
};

typedef struct move_t move_t;

      

MORE EDITORS: firstMove is a global list of moves. AddToList function as above:

move_t *addToList(game_t game, move_t *list, move_t *move, int color) {
    int kx, ky;
    game_t copy;
    copyGame(game, &copy);
    makeMove(&copy, *move);

    kx = copy.data[2];
    ky = copy.data[3];
    if (color) {
        kx = copy.data[0];
        ky = copy.data[1];
    }
    if (isAttacked(&copy, kx, ky, color) == 0) {
        move->next = list;
        list = move;
    } else {
        free(move);
    }
    freeGame(copy);
    return list;
}

      

If the potential movement is illegal, i.e. the courier king is in control, the move is not added to the list and released. Perhaps the problem is the firstMove release that is happening throughout the program.

Why does Valgrind think the malloc problem seems to be a problem? I think this is good. How to interpret this?

+3


source to share





All Articles