下面的代码在fclose()通话中中断。
void output_gauss_transform(char* filename, char* mode, double** T,
double shift, int len)
{
FILE* fp;
printf("Outputting gauss transform to %s.\n", filename);
if ((fp = fopen(filename, mode)) == NULL){
perror("Could not open file");
return;
}
int i;
for (i = 0; i < len; ++i) {
fprintf(fp, "%lf %lf\n", T[0][i], T[1][i] + shift);
}
if (fclose(fp)){
printf("error closing\n");
}
}
Run Code Online (Sandbox Code Playgroud)
glibc 给我这个错误,以及内存映射。
*** glibc detected *** [sourcedir]/.libs/lt-launcher: free(): invalid next size (normal): 0x0821da38 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb739dee2]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0xb738d424]
/src/.libs/libfile_util.so.0(output_gauss_transform+0xa9)[0xb77b5859]
/src/.libs/lt-launcher[0x804a0f9]
/src/.libs/lt-launcher[0x804a2a5]
/src/.libs/lt-launcher[0x804983b]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb73414d3]
/src/.libs/lt-launcher[0x8049915]
Run Code Online (Sandbox Code Playgroud)
当尝试使用进行调试时valgrind,我没有任何错误,它输出以下内容。到底是怎么回事?
==30396== HEAP SUMMARY:
==30396== in use at exit: 0 bytes in 0 blocks
==30396== total heap usage: 1,059 allocs, 1,059 frees, 78,149 bytes allocated
==30396==
==30396== All heap blocks were freed -- no leaks are possible
==30396==
==30396== For counts of detected and suppressed errors, rerun with: -v
==30396== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Run Code Online (Sandbox Code Playgroud)
编辑:运行valgrind有-v,我得到这个东西在最后。也许这与正在发生的事情有关?
--31325-- REDIR: 0x454cac0 (operator delete(void*)) redirected to 0x402bb98 (operator delete(void*))
Run Code Online (Sandbox Code Playgroud)
此代码是受害者,您需要找到肇事者。当您调用时fclose,某些结构被释放。那时,代码发现空闲池已损坏并报告错误。但是,破坏空闲池的是其他一些代码块,而不是该代码。
导致此错误的最常见原因是释放同一块内存两次,并在释放后访问一块内存。奇怪的valgrind是无法捕获此错误,因为这些恰恰是通常捕获的错误类型。