简体   繁体   中英

Can't find memory leaks in c

I run createNewBoard which calls createNewMatrix and the I exit the program, and I have a memory leak I can't find. Here's the code

BoardP createNewBoard(int width, int high)
{
    BoardP board = (BoardP) malloc(sizeof(Board));

    if (board == NULL)
    {
        reportError(MEM_OUT);
        return NULL;
    }
    board->height = high;
    board->width = width;
    board->matrix = createNewMatrix(width,high);
    printf("%c",board->matrix[1][1]);
    if (board->matrix == NULL)
    {
        reportError(MEM_OUT);
        freeBoard(board);
    return NULL;
    }
return board;
}

static char** createNewMatrix(int width, int height){
    char** newMatrix = (char**) calloc(height,sizeof(char*));
    int i;
    for (i=0; i<height; i++)
    {
        newMatrix[i] = (char*) calloc(width,sizeof(char)); //LINE 71
        if (newMatrix[i] == NULL)
        {
            int j;
            for (j=0; j<i; j++)
            {
                free(newMatrix[j]);
            }
            free(newMatrix);
            return NULL;
        }
    }
    return newMatrix;
 }

It is driving me crazy. All I do is to create a pointer to Board struct (which holds to integers and a two dimensional pointer array) and I have a memory leak. Here's the message:

==10436== HEAP SUMMARY:
==10436==     in use at exit: 100 bytes in 10 blocks
==10436==   total heap usage: 12 allocs, 2 frees, 196 bytes allocated
==10436== 
==10436== 100 bytes in 10 blocks are definitely lost in loss record 1 of 1
==10436==    at 0x4C2380C: calloc (vg_replace_malloc.c:467)
==10436==    by 0x4008C6: createNewMatrix (Board.c:71)
==10436==    by 0x40081E: createNewBoard (Board.c:55)
==10436==    by 0x4007C6: createNewDefaultBoard (Board.c:37)
==10436==    by 0x400F0C: main (PlayBoard.c:11)
==10436== 
==10436== LEAK SUMMARY:
==10436==    definitely lost: 100 bytes in 10 blocks
==10436==    indirectly lost: 0 bytes in 0 blocks
==10436==      possibly lost: 0 bytes in 0 blocks
==10436==    still reachable: 0 bytes in 0 blocks
==10436==         suppressed: 0 bytes in 0 blocks

It points me to line 71, which calls calloc for a line in the matrix. When the program exits it calls freeBoard:

void freeBoard(BoardP board)
{
    if (board != NULL)
    {
        if(board->matrix != NULL)
        {
            free(board->matrix);
        }
        free(board);
    }
}

Any ideas why I have a memory leak? Thanks!

You have to free the individual lines, before freeing matrix .

for (i=0; i<height; i++)
{
    free(board->matrix[i]);
}

For the Matrix you allocate memory for all elements but you don't deallocate them in the function freeBoard() .

I don't see the confusion, you clearly don't free any of the height arrays of size width*sizeof(char) . Going by the output, both height and width are 10.

Your free free(board->matrix) needs to free the matrix just how you free it in the NULL check of createNewMatrix .

void freeBoard(BoardP board)
{
    if (board != NULL)
    {
        if(board->matrix != NULL)
        {
            for (int i = 0; i < board->height; i++)
            {
                free(board->matrix[i]);
            }
            free(board->matrix);
        }
        free(board);
    }
}

It may be easier to create a freeMatrix function accepting a height, or create a matrix structure that maintains its own height and width.

board->matrix is a pointer to a pointer, which you have allocated dynamically. First you have used calloc to allocate the first dimension and for each index you used calloc to allocate the other dimension. Therefore you have an array of pointers which points to blocks of memory, and you have to free each block, indicated by each index.

In your code, you simply free the first dimension, each location of which points to the different blocks. On the other hand these blocks in the 2nd dimension are not freed.

You should do something like this:

void freeBoard(BoardP board)
{
    int i;
    if (board != NULL)
    {
        if(board->matrix != NULL)
        {
            for (i=0; i<board->height; i++);
               free(board->matrix[i]);
        }
        free(board);
    }
}

I have tried thing in this post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM