Reputation: 13309
I have a code like this
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);
}
}
}
}
}
}
The only place that i use the verboseBuf is inside the final if loop. but i get
*** glibc detected *** ./test: double free or corruption (!prev): 0x13c13290 ***
But how can be freeing it twice if i only use it in one place? and everytime i use it, i free it.
I tried using addr2line to find the place where it was freed previously but all got was a ??:0
.
Upvotes: 0
Views: 748
Reputation: 1153
Make verboseBuf[currSize + 1] = '\0';
as verboseBuf[currSize] = '\0';
Upvotes: 0
Reputation: 34563
That message doesn't specifically mean that you freed something twice, it means that glibc detected heap corruption, and freeing things twice is one common cause of that, but not the only one.
In this case, the line
verboseBuf[currSize + 1] = '\0';
is overflowing the end of your buffer, corrupting whatever bookkeeping data the allocator stored after it. Remove the +1 and it should work.
Upvotes: 3
Reputation: 16290
This line is writing one byte past the end of your buffer.
verboseBuf[currSize + 1] = '\0';
Upvotes: 7