2

下面的代码在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");
    }
}

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]

尝试使用 进行调试时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)

编辑:运行valgrind-v我最后得到了这个东西。也许它与正在发生的事情有关?

 --31325-- REDIR: 0x454cac0 (operator delete(void*)) redirected to 0x402bb98 (operator delete(void*))
4

1 回答 1

5

这段代码是受害者,你需要找到肇事者。当你调用时fclose,一些结构被释放。此时,代码发现空闲池已损坏并报告错误。但是,破坏空闲池的是其他代码块,而不是这段代码。

此错误的最常见原因是两次释放相同的内存块并在释放内存块后访问它。valgrind无法捕捉到这一点很奇怪,因为这些正是它通常捕捉到的那种错误。

于 2013-01-31T22:13:17.023 回答