双重免费或内存损坏

Pra*_*van 0 c++ free

我有这样的代码

char *verboseBuf = NULL;
if(somethin){
    for(a loop){
        for(another loop){
            if(somethin else){
                if(curl execution){
                    if(fail){
                        verboseBuf = (char *) malloc(sizeof(char) * (currSize +1));
                        fread(verboseBuf, 1, currSize, verboseFd);
                        verboseBuf[currSize + 1] = '\0';
                        string verbose = verboseBuf;
                        free(verboseBuf);
                    }   
                }   
            }   
        }   
    }   
}
Run Code Online (Sandbox Code Playgroud)

我使用verboseBuf的唯一地方是在最终的if循环中.但我明白了

*** glibc detected *** ./test: double free or corruption (!prev): 0x13c13290 ***
Run Code Online (Sandbox Code Playgroud)

但是,如果我只在一个地方使用它,怎么可以释放它两次呢?每次我使用它,我都会释放它.我尝试使用addr2line来找到之前被释放的地方,但所有得到的都是??:0.

Sti*_*sis 7

该行在缓冲区末尾写入一个字节.

verboseBuf[currSize + 1] = '\0';
Run Code Online (Sandbox Code Playgroud)