首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测到glibc -双重释放或损坏

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

Stack Overflow用户
提问于 2010-08-28 18:39:30
回答 4查看 11.2K关注 0票数 3

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

代码语言:javascript
复制
*** glibc detected *** ./b: double free or corruption (top): 0x092ae138 ***  

我找不到答案,希望你能帮忙。

下面是main()的调用

代码语言:javascript
复制
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;
        }
    }
}

以下是Free函数:

代码语言:javascript
复制
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**
}

这就是我如何创建一个新的板子:

代码语言:javascript
复制
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:

代码语言:javascript
复制
typedef struct Board* BoardP;
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-08-28 18:50:56

您必须释放已分配的内存,并且不再希望保留引用。从您的代码中,我可以看到下面这一行。

代码语言:javascript
复制
theBoard->_board = newBoard->_board;

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

示例代码:

代码语言:javascript
复制
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
}
票数 6
EN

Stack Overflow用户

发布于 2010-08-28 19:02:08

试试这个:

代码语言:javascript
复制
copyData(theBoard, newBoard);
/* swap the _board pointers */
char * b = theBoard->_board;
theBoard->_board = newBoard->_board;
newBoard->_board = b;
FreeBoard(newBoard);  /* cleanup the temp struct and the old array */
票数 1
EN

Stack Overflow用户

发布于 2010-08-28 19:45:10

此错误说明您正在尝试释放已被您释放的内存。我在这里怀疑的是这段代码

代码语言:javascript
复制
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);

一旦你释放了结构的一部分(&freeBoardArray->_ problem.Why );,然后你释放了整个结构(Board);,它看起来像是我导致了板子,你传递了_board指针的地址?我在同一行代码上写了代码,这会导致问题。

代码语言:javascript
复制
struct a{
    int * next;

};int main(){

代码语言:javascript
复制
    struct a *aptr = (struct a *)malloc(sizeof(struct a));
    aptr->next=(int *)malloc(5*sizeof(int));
    free(&aptr->next);
    free(aptr);
    return 0;

}

此代码将导致与您所显示的相同的问题。现在,在从free(&aptr->next)中删除'&‘后再次尝试此代码,;statement.It将正常工作。所以我想你得到了你需要修改的地方的线索。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3590423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档