简体   繁体   English

在 c 中找不到 memory 泄漏

[英]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.我运行调用 createNewMatrix 的 createNewBoard 并退出程序,但我找不到 memory 泄漏。 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.我所做的只是创建一个指向 Board 结构的指针(它包含整数和二维指针数组),并且我有一个 memory 泄漏。 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.它指向第 71 行,它调用 calloc 来表示矩阵中的一行。 When the program exits it calls freeBoard:当程序退出时,它会调用 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?任何想法为什么我有 memory 泄漏? Thanks!谢谢!

You have to free the individual lines, before freeing matrix .在释放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() .对于矩阵,您为所有元素分配 memory 但您没有在 function freeBoard()中取消分配它们。

I don't see the confusion, you clearly don't free any of the height arrays of size width*sizeof(char) .我没有看到混乱,您显然没有释放任何大小width*sizeof(char)height arrays 。 Going by the output, both height and width are 10.通过output, heightwidth都是10。

Your free free(board->matrix) needs to free the matrix just how you free it in the NULL check of createNewMatrix .您的免费free(board->matrix)需要在createNewMatrix的 NULL 检查中释放矩阵。

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.创建一个接受高度的freeMatrix function 或创建一个保持其自身高度和宽度的矩阵结构可能更容易。

board->matrix is a pointer to a pointer, which you have allocated dynamically. board->matrix是指向您已动态分配的指针的指针。 First you have used calloc to allocate the first dimension and for each index you used calloc to allocate the other dimension.首先,您使用calloc分配第一个维度,并为每个索引使用calloc分配另一个维度。 Therefore you have an array of pointers which points to blocks of memory, and you have to free each block, indicated by each index.因此,您有一个指向 memory 块的指针数组,并且您必须释放每个块,由每个索引指示。

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在这篇文章中尝试过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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