c - 检测到 glibc - 双重释放或损坏

标签 c memory-management

这可能有点长,所以我很抱歉。 考虑以下代码(我从中留下了一些不相关的部分)。此代码接收指向结构 (BoardP theBoard) 的指针、x 和 y 坐标和一个值。 目标是将值放在结构中找到的二维数组中。 如果坐标超出范围,我必须增加表的大小,将旧数据复制到新数据并将值放在它的位置。 很好,这段代码在第一次调用时有效,但在第二次调用中它崩溃并写入:

*** glibc detected *** ./b: double free or corruption (top): 0x092ae138 ***  

我找不到答案,希望你能帮忙。
这些是来自 main() 的调用

BoardP p = CreateNewBoard(10,10);
PutBoardSquare(p,10,5,'X');
PutBoardSquare(p,5,10,'O');

Boolean PutBoardSquare(BoardP theBoard, int X, int Y, char val) {

    if (inBounds(X,Y,theBoard->_rows,theBoard->_cols)) {
        theBoard->_board[X * theBoard->_cols + Y] = val;
        return TRUE;
    }
    else {
        int newRows = (X>=theBoard->_rows) ? (2*X) : theBoard->_rows;
        int newCols = (Y>=theBoard->_cols) ? (2*Y) : theBoard->_cols;
        BoardP newBoard = CreateNewBoard(newCols,newRows);  //this creates a new Board with the new dimensions
        if (newBoard == NULL) {
            //ReportError(MEM_OUT);
            return FALSE;
        }
        else {
            copyData(theBoard,newBoard);
            freeBoardArray(&theBoard->_board[0]); //free old array
            theBoard->_board = newBoard->_board;  //old array point to new array
            FreeBoard(newBoard);  //free the temp copy THIS CAUSES THE PROBLEM  
            PutBoardSquare(theBoard,X,Y,val);//recursion, will be in bounds now
            return TRUE;
        }
    }
}

这些是免费功能:

void FreeBoard(BoardP board) {
    if (board != NULL) {
        printf("FREE 1\n");
        //free the board array:
        if (board->_board != NULL) {
            printf("FREE 2\n");
            freeBoardArray(&board->_board[0]);
            printf("FREE 3\n");
        }
        free(board);
    }
}

static void freeBoardArray(char * arrP) {
    free(arrP);   //**PROGRAM CRASH HERE**
}

这就是我创建新板的方式:

BoardP CreateNewBoard(int width, int high) {
    BoardP board = (BoardP) malloc(sizeof(Board));
    if (board != NULL) {
        board->_board = allocateBoardArray(high,width);
        if ( board->_board == NULL) {
            FreeBoard(board);
            //TODO make file ReportError(MEM_OUT);
            return NULL;
        }
        initializeBoard(board,high,width,X_SIGN,SPACE);
        return board;
    }
    else {
        FreeBoard(board);
        //TODO make file ReportError(MEM_OUT);
        return NULL;
    }
}

static char* allocateBoardArray(int row, int col) {
    char* newBoard = (char*) malloc(row * col * sizeof(char));

    if (newBoard == NULL) {
        return NULL;
    }
    return newBoard;
}

这是 BoardP:

typedef struct Board* BoardP;

最佳答案

你必须释放你已经分配的内存并且不再想持有一个引用。 从您的代码中我可以看到以下行。

theBoard->_board = newBoard->_board;

现在您维护对已分配指针的引用,然后释放该指针本身。

示例代码:

char *foo()
{
char *ref1;
char *ref2;
ref1 = malloc(256);
ref2=ref1;// Holding reference to a pointer in another pointer
strcpy(ref1,"stackoverflow");
printf("%s %s",ref1,ref2); // This prints stackoverflow twice
free(ref1); // This is valid but you can access ref2 or ref1 after this point
return ref2; /// This will cause problems
}

关于c - 检测到 glibc - 双重释放或损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3590423/

相关文章:

c++ - 如何在源代码中查找搜索词

c - 强制另一个文件定义一个函数

c - 为什么在C中使用malloc时要指定大小?

c++ - 临时字符串的内存分配

c - 为 DMA 使用保留内存

c - 释放 pthread_create 中指定的函数中的参数

c - 如何在 C 程序中打印与输入字符最接近的字母数字?

memory - 了解 CUDA 中的内存使用情况

iphone - iOS推送后释放

C++ Arena 分配器可用内存始终是一半