C++ 双重释放或内存损坏
我有这样的代码C++ 双重释放或内存损坏,c++,free,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(ch
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);
}
}
}
}
}
}
我使用verboseBuf的唯一地方是在最终的if循环中。但我明白了
*** glibc detected *** ./test: double free or corruption (!prev): 0x13c13290 ***
但是如果我只在一个地方使用它,怎么能释放它两次呢?每次我使用它,我都会释放它。
我尝试使用addr2line来查找以前释放它的位置,但得到的只是一个
?:0
这一行写入的字节超过了缓冲区的末尾
verboseBuf[currSize + 1] = '\0';
这条消息并不特别意味着你释放了两次东西,它意味着glibc检测到了堆损坏,两次释放东西是一个常见的原因,但不是唯一的原因 在这种情况下,该行
verboseBuf[currSize + 1] = '\0';
溢出缓冲区的末尾,损坏分配器存储在其后面的任何簿记数据。删除+1,它应该可以工作。Make
verboseBuf[currSize+1]='\0'代码>asverboseBuf[currSize]='\0'代码> 如果你在C++中编码,你应该更喜欢使用新的和删除。此外,您可能需要考虑使用RIII或其他更为实用的C++内存管理技术。