gpt4 book ai didi

c - free() : invalid next size (normal) on fclose. 但当 Valgrind 运行时不会

转载 作者:行者123 更新时间:2023-12-03 15:37:43 38 4
gpt4 key购买 nike

下面的代码在 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*))

最佳答案

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

导致此错误的最常见原因是两次释放同一 block 内存,并在释放后访问该内存块。奇怪的是 valgrind 无法捕获此错误,因为这些正是它通常捕获的错误类型。

关于c - free() : invalid next size (normal) on fclose. 但当 Valgrind 运行时不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14635945/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com