Thread: Need help by debugging Memory Leak

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    67

    Need help by debugging Memory Leak

    Here is a link to full chess program:
    GitHub - SuchtyTV/Odin

    Trying to debug this code using Valgrind shows huge memory leaks, which I cannot locate.

    I suspect the error to be here:

    Code:
        #include "calculation.h"#include "odinutilities.h"
    #include "chess.h"
    #include "board.h"
    #include <stdio.h>
    #include <string.h>
    
    
    double calculateBoard(BOARD_STATE* pstate){
        double sum = 0;
        for(int i = 0; i < 8; i++){
            for(int k = 0; k<8; k++){
                if(pstate->board[i][k] == king){
                    sum += 100;
                    continue;
                } else if(pstate->board[i][k] == -king) {
                    sum += -100;
                    continue;
                }
                sum += pstate->board[i][k];
            }
        }
        return sum;
    
    
        /*
         * Add Neural Network
         */
    }
    
    
    int init_ODIN_Node(ODIN_Node* pnode, BOARD_STATE state){
        pnode->state = state;
        pnode->leaves_size = INITIAL_LEAVES_FOR_NODE;
        pnode->leaves = malloc(sizeof(ODIN_Node)*INITIAL_LEAVES_FOR_NODE);
        if(pnode->leaves == NULL){
            printf("MALLOC FAILED\n");
            return 0;
        }
        pnode->used_leaves_size = 0;
        return 1;
    }
    
    
    int add_Leaf_Copy(ODIN_Node* psrc, ODIN_Node* pto_be_added){
        if (psrc->leaves_size <= psrc->used_leaves_size) {
            ODIN_Node* p = realloc(psrc->leaves,
                    (size_t)(2 * (psrc->leaves_size) * sizeof(ODIN_Node)));
            psrc->leaves_size = 2* (psrc->leaves_size);
            if (!p) {
                return 0;
            } else {
                psrc->leaves = p;
            }
        }
        memcpy(&(psrc->leaves[psrc->used_leaves_size++]),pto_be_added,sizeof(ODIN_Node));
        return 1;
    }
    
    
    void free_ODIN_NODE(ODIN_Node* pnode){
        for(int i = 0; i < pnode->used_leaves_size; i++){
            free_ODIN_NODE(&(pnode->leaves[i]));
        }
        if(pnode->leaves_size != 0){
            free(pnode->leaves);
        }
        pnode->used_leaves_size = 0;
        pnode->leaves_size = 0;
    }
    
    
    double minimax(ODIN_Node* pnode, int depth, char color, double alpha, double beta){
        if(depth == 0 || is_game_over(&(pnode->state))){
            pnode->score = calculateBoard(&(pnode->state));
            return calculateBoard(&(pnode->state));
        }
    
    
        double maxeval = -WORST_POSSIBLE_SCORE;
        MOVE_DATA move_data = return_all_legal_moves(&(pnode->state));
    
    
        switch(color){
        case white: ; //This is an empty instruction
            for(int i = 0; i < move_data.used_array_size; i++){
                BOARD_STATE state = make_move_with_copy(&(pnode->state), move_data.moves[i]);
                ODIN_Node new_node;
                init_ODIN_Node(&new_node, state);
                add_Leaf_Copy(pnode, &new_node);
                double eval = minimax(&(pnode->leaves[i]), depth-1,(char)(-color), alpha, beta);
                maxeval = max(maxeval, eval);
                alpha = max(alpha, eval);
                if(beta <= alpha){
                    break;
                }
            }
            break;
        case black: ; //This is an empty instruction
            maxeval = WORST_POSSIBLE_SCORE;
            for(int i = 0; i < move_data.used_array_size; i++){
                BOARD_STATE state = make_move_with_copy(&(pnode->state), move_data.moves[i]);
                ODIN_Node new_node;
                init_ODIN_Node(&new_node, state);
                add_Leaf_Copy(pnode, &new_node);
                double eval = minimax(&(pnode->leaves[i]), depth-1,(char)(-color), alpha, beta);
                maxeval = min(maxeval, eval);
                beta = min(beta, eval);
                if(beta <= alpha){
                    break;
                }
            }
            break;
        }
        pnode->score = maxeval;
        delete_MOVE_DATA(&move_data);
        return maxeval;
    }
    However, I see no certain flaw in the logic.

    Minimax is the function creating the tree to calculate the next move and therby evaluating the board. Therefore first all possible moves for the board will be generated. After this, every move will be played on the current board. Then the resulting board will be added as a new leaf/child for the tree.

    What are your impressions?

    Thank you!

    Valgrind:

    ==19752== 404 (320 direct, 84 indirect) bytes in 1 blocks are definitely lost in loss record 98 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x10941A: store_all_moves_of_rook (board.c:162)
    ==19752== by 0x10959A: store_all_moves_of_queen (board.c:202)
    ==19752== by 0x109CB5: return_all_moves (board.c:381)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 407 (320 direct, 87 indirect) bytes in 1 blocks are definitely lost in loss record 99 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x109170: store_all_moves_of_knight (board.c:112)
    ==19752== by 0x109BEF: return_all_moves (board.c:360)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 437 (320 direct, 117 indirect) bytes in 1 blocks are definitely lost in loss record 101 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x10954D: store_all_moves_of_bishop (board.c:191)
    ==19752== by 0x1095C7: store_all_moves_of_queen (board.c:207)
    ==19752== by 0x109CB5: return_all_moves (board.c:381)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752==
    ==19752== 781 (640 direct, 141 indirect) bytes in 1 blocks are definitely lost in loss record 110 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x109510: store_all_moves_of_bishop (board.c:186)
    ==19752== by 0x109C31: return_all_moves (board.c:367)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 814 (640 direct, 174 indirect) bytes in 2 blocks are definitely lost in loss record 112 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x109170: store_all_moves_of_knight (board.c:112)
    ==19752== by 0x109BEF: return_all_moves (board.c:360)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 874 (640 direct, 234 indirect) bytes in 2 blocks are definitely lost in loss record 113 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x108EC8: store_all_moves_of_pawn (board.c:51)
    ==19752== by 0x109BAD: return_all_moves (board.c:353)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 1,712 (1,280 direct, 432 indirect) bytes in 4 blocks are definitely lost in loss record 122 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x1094D3: store_all_moves_of_bishop (board.c:181)
    ==19752== by 0x109C31: return_all_moves (board.c:367)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 2,855 (2,240 direct, 615 indirect) bytes in 7 blocks are definitely lost in loss record 127 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x109393: store_all_moves_of_rook (board.c:153)
    ==19752== by 0x10959A: store_all_moves_of_queen (board.c:202)
    ==19752== by 0x109CB5: return_all_moves (board.c:381)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752==
    ==19752== 3,002 (2,240 direct, 762 indirect) bytes in 7 blocks are definitely lost in loss record 129 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x10954D: store_all_moves_of_bishop (board.c:191)
    ==19752== by 0x109C31: return_all_moves (board.c:367)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 4,624 (3,520 direct, 1,104 indirect) bytes in 11 blocks are definitely lost in loss record 135 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109047: store_all_moves_of_pawn (board.c:78)
    ==19752== by 0x109BAD: return_all_moves (board.c:353)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 6,725 (5,120 direct, 1,605 indirect) bytes in 16 blocks are definitely lost in loss record 137 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x109510: store_all_moves_of_bishop (board.c:186)
    ==19752== by 0x1095C7: store_all_moves_of_queen (board.c:207)
    ==19752== by 0x109CB5: return_all_moves (board.c:381)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752==
    ==19752== 7,985 (6,080 direct, 1,905 indirect) bytes in 19 blocks are definitely lost in loss record 138 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x108F4D: store_all_moves_of_pawn (board.c:61)
    ==19752== by 0x109BAD: return_all_moves (board.c:353)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 9,706 (7,360 direct, 2,346 indirect) bytes in 23 blocks are definitely lost in loss record 141 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x10930D: store_all_moves_of_rook (board.c:144)
    ==19752== by 0x109C73: return_all_moves (board.c:374)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 11,208 (8,640 direct, 2,568 indirect) bytes in 25 blocks are definitely lost in loss record 143 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x109393: store_all_moves_of_rook (board.c:153)
    ==19752== by 0x109C73: return_all_moves (board.c:374)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 11,775 (9,600 direct, 2,175 indirect) bytes in 21 blocks are definitely lost in loss record 145 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x109170: store_all_moves_of_knight (board.c:112)
    ==19752== by 0x109BEF: return_all_moves (board.c:360)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 13,130 (10,880 direct, 2,250 indirect) bytes in 17 blocks are definitely lost in loss record 146 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x109711: store_all_moves_of_king (board.c:230)
    ==19752== by 0x109CF3: return_all_moves (board.c:388)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 16,266 (13,440 direct, 2,826 indirect) bytes in 21 blocks are definitely lost in loss record 147 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x109493: store_all_moves_of_bishop (board.c:176)
    ==19752== by 0x109C31: return_all_moves (board.c:367)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 18,456 (15,360 direct, 3,096 indirect) bytes in 24 blocks are definitely lost in loss record 150 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A073: generate_all_moves_of_bishops_direction (board.c:472)
    ==19752== by 0x109510: store_all_moves_of_bishop (board.c:186)
    ==19752== by 0x109C31: return_all_moves (board.c:367)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 22,362 (18,240 direct, 4,122 indirect) bytes in 30 blocks are definitely lost in loss record 151 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x109510: store_all_moves_of_bishop (board.c:186)
    ==19752== by 0x109C31: return_all_moves (board.c:367)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 28,407 (22,080 direct, 6,327 indirect) bytes in 69 blocks are definitely lost in loss record 152 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x10930D: store_all_moves_of_rook (board.c:144)
    ==19752== by 0x10959A: store_all_moves_of_queen (board.c:202)
    ==19752== by 0x109CB5: return_all_moves (board.c:381)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752==
    ==19752== 30,809 (24,320 direct, 6,489 indirect) bytes in 76 blocks are definitely lost in loss record 153 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x10941A: store_all_moves_of_rook (board.c:162)
    ==19752== by 0x10959A: store_all_moves_of_queen (board.c:202)
    ==19752== by 0x109CB5: return_all_moves (board.c:381)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752==
    ==19752== 38,926 (29,440 direct, 9,486 indirect) bytes in 92 blocks are definitely lost in loss record 154 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x108F4D: store_all_moves_of_pawn (board.c:61)
    ==19752== by 0x109BAD: return_all_moves (board.c:353)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 49,708 (39,040 direct, 10,668 indirect) bytes in 122 blocks are definitely lost in loss record 155 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109FF9: generate_all_moves_of_bishops_direction (board.c:468)
    ==19752== by 0x109493: store_all_moves_of_bishop (board.c:176)
    ==19752== by 0x1095C7: store_all_moves_of_queen (board.c:207)
    ==19752== by 0x109CB5: return_all_moves (board.c:381)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752==
    ==19752== 71,136 (53,760 direct, 17,376 indirect) bytes in 168 blocks are definitely lost in loss record 156 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x109047: store_all_moves_of_pawn (board.c:78)
    ==19752== by 0x109BAD: return_all_moves (board.c:353)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B4BC: main (main.c:43)
    ==19752==
    ==19752== 91,304 (72,320 direct, 18,984 indirect) bytes in 189 blocks are definitely lost in loss record 157 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x109170: store_all_moves_of_knight (board.c:112)
    ==19752== by 0x109BEF: return_all_moves (board.c:360)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== 129,036 (97,920 direct, 31,116 indirect) bytes in 306 blocks are definitely lost in loss record 158 of 158
    ==19752== at 0x4C31D2F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19752== by 0x109EEA: add_move_to_data (board.c:440)
    ==19752== by 0x10A17B: add_move_if_unused_by_equal_color (board.c:497)
    ==19752== by 0x10930D: store_all_moves_of_rook (board.c:144)
    ==19752== by 0x109C73: return_all_moves (board.c:374)
    ==19752== by 0x10A64A: filter_all_legal_moves (board.c:580)
    ==19752== by 0x1099EE: return_all_legal_moves (board.c:316)
    ==19752== by 0x10AF6F: minimax (calculation.c:74)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752== by 0x10B254: minimax (calculation.c:98)
    ==19752== by 0x10B090: minimax (calculation.c:83)
    ==19752==
    ==19752== LEAK SUMMARY:
    ==19752== definitely lost: 445,760 bytes in 1,255 blocks
    ==19752== indirectly lost: 127,089 bytes in 42,363 blocks
    ==19752== possibly lost: 0 bytes in 0 blocks
    ==19752== still reachable: 4,907 bytes in 164 blocks
    ==19752== suppressed: 0 bytes in 0 blocks
    ==19752== Reachable blocks (those to which a pointer was found) are not shown.
    ==19752== To see them, rerun with: --leak-check=full --show-leak-kinds=all
    ==19752==
    ==19752== ERROR SUMMARY: 26 errors from 26 contexts (suppressed: 0 from 0)
    ==19752== ERROR SUMMARY: 26 errors from 26 contexts (suppressed: 0 from 0)
    Aborted (core dumped)
    Last edited by SuchtyTV; 08-11-2019 at 04:28 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    main() has a call to free_ODIN_NODE(&node);

    These do not.
    In minimax - twice
    Code:
    			ODIN_Node new_node;
    			init_ODIN_Node(&new_node, state);
    			add_Leaf_Copy(pnode, &new_node);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    67
    I think it should work. I make a shallow copy of the Node. The copy will be added and the original will decay.
    However all pointers are now stored in the tree?
    Am I missing something?
    Last edited by SuchtyTV; 08-12-2019 at 03:34 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    This is C there is no garbage collection.

    I see no where that new_node is freed and likely the check program does not either.

    Tim S.
    Last edited by stahta01; 08-12-2019 at 03:58 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    67
    Quote Originally Posted by stahta01 View Post
    This is C there is no garbage collection.

    I see no where that new_node is freed and likely the check program does not either.

    Tim S.
    Code:
    int init_ODIN_Node(ODIN_Node* pnode, BOARD_STATE state){
        pnode->state = state;
        pnode->leaves_size = INITIAL_LEAVES_FOR_NODE;
        pnode->leaves = malloc(sizeof(ODIN_Node)*INITIAL_LEAVES_FOR_NODE);
        if(pnode->leaves == NULL){
            printf("MALLOC FAILED\n");
            return 0;
        }
        pnode->used_leaves_size = 0;
        return 1;
    }
    
    
    int add_Leaf_Copy(ODIN_Node* psrc, ODIN_Node* pto_be_added){
        if (psrc->leaves_size <= psrc->used_leaves_size) {
            ODIN_Node* p = realloc(psrc->leaves,
                    (size_t)(2 * (psrc->leaves_size) * sizeof(ODIN_Node)));
            psrc->leaves_size = 2* (psrc->leaves_size);
            if (!p) {
                return 0;
            } else {
                psrc->leaves = p;
            }
        }
        memcpy(&(psrc->leaves[psrc->used_leaves_size++]),pto_be_added,sizeof(ODIN_Node));
        return 1;
    }
    
    
    void free_ODIN_NODE(ODIN_Node* pnode){
        for(int i = 0; i < pnode->used_leaves_size; i++){
            free_ODIN_NODE(&(pnode->leaves[i]));
        }
        if(pnode->leaves_size != 0){
            free(pnode->leaves);
        }
        pnode->used_leaves_size = 0;
        pnode->leaves_size = 0; 
    }
    First newnode will be initalized on stack:

    Code:
    int init_ODIN_Node(ODIN_Node* pnode, BOARD_STATE state){
        pnode->state = state;
        pnode->leaves_size = INITIAL_LEAVES_FOR_NODE;
        pnode->leaves = malloc(sizeof(ODIN_Node)*INITIAL_LEAVES_FOR_NODE);
        if(pnode->leaves == NULL){
            printf("MALLOC FAILED\n");
            return 0;
        }
        pnode->used_leaves_size = 0;
        return 1;
    }
    Then it will be copied, all its pointer therefore are no transferred to the heap. Newnode now can decay and it will be deallocated from the stack, when the function returns.
    Its pointers are still saved in the tree.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    "Newnode now can decay"; I consider these words to mean that you think C has garbage collection!

    What do you mean by "decay"?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    67
    Quote Originally Posted by stahta01 View Post
    "Newnode now can decay"; I consider these words to mean that you think C has garbage collection!

    What do you mean by "decay"?

    Tim S.
    Newnode will be initalised on stack.
    Then I make a copy of it using &newnode
    When now the function returns newnode will be deleted, since it is no longer needed it in the stack.
    if I woulld call free(&newnode) I would get undefined bevaiour since I am freeing on stack. (Look at minimax)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  2. memory leak
    By markucd in forum C++ Programming
    Replies: 14
    Last Post: 06-13-2006, 11:14 AM
  3. Is this a memory leak?
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-20-2005, 01:15 PM
  4. A memory leak (I think...)
    By Stevek in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 03:09 PM

Tags for this Thread