【问题标题】:Valgrind reports different numbers of allocs and frees, yet all blocks freedValgrind 报告不同数量的分配和释放,但所有块都被释放
【发布时间】:2019-11-27 12:26:13
【问题描述】:

我使用valgrind 运行一个可执行文件。可执行文件是用 C 语言编写的,它调用一个包含 Fortran 代码并使用 MPI (MPICH) 的大型数字 C 库。

$ valgrind --leak-check=full --show-leak-kinds=all ./ex1
==13877== Memcheck, a memory error detector
==13877== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13877== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13877== Command: ./ex1
==13877== 
==13877== 
==13877== HEAP SUMMARY:
==13877==     in use at exit: 0 bytes in 0 blocks
==13877==   total heap usage: 2,057 allocs, 2,046 frees, 4,812,368 bytes allocated
==13877== 
==13877== All heap blocks were freed -- no leaks are possible
==13877== 
==13877== For counts of detected and suppressed errors, rerun with: -v
==13877== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

请注意,在堆摘要中,分配的次数多于释放的次数,但没有报告泄漏(甚至“仍然可访问”的泄漏)。我不明白这怎么可能。 这是不一致的输出,还是有合法的机制可以产生这种情况?

使用-v 运行会给出包含此警告的输出:

==14266== WARNING: new redirection conflicts with existing -- ignoring it
--14266--     old: 0x0401f2f0 (strlen              ) R-> (0000.0) 0x58060901 ???
--14266--     new: 0x0401f2f0 (strlen              ) R-> (2007.0) 0x04c32db0 strlen

【问题讨论】:

  • 也许 realloc() 算作一个分配(而您的运行有 11 个重新分配)?
  • 这不是我所期望的,天真。这显示了 2 个分配和 2 个释放:printf "#include <stdlib.h>\n int main(){int* a = malloc(10); int* b = realloc(a,20); free(b);}" > t.c && gcc -O0 t.c && valgrind ./a.out
  • 这个问题与 MPI 有什么关系?
  • 它正在运行一个依赖于 MPI 的代码,因此可以想象 MPI 实现可能涉及此行为。

标签: c fortran mpi valgrind heap-memory


【解决方案1】:

正如怀疑的那样,这似乎是 MPI 实现 MPICH(provides custom logic to valgrind)的问题,因为它使用非标准堆分配过程。

特别是,可以使用简单的 MPI 程序重现该问题。

#include <mpi.h>
int main(int argc, char **argv)
{
    MPI_Init(&argc,&argv);
    MPI_Finalize();
}
==7291== Memcheck, a memory error detector
==7291== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7291== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==7291== Command: ./a.out
==7291==
==7291==
==7291== HEAP SUMMARY:
==7291==     in use at exit: 0 bytes in 0 blocks
==7291==   total heap usage: 1,979 allocs, 1,974 frees, 4,720,483 bytes allocated
==7291==
==7291== All heap blocks were freed -- no leaks are possible
==7291==
==7291== For counts of detected and suppressed errors, rerun with: -v
==7291== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

这是使用 MPICH 3.1b1,配置为

$ ./configure --prefix=[REMOVED] MAKE=/usr/bin/make --libdir=[REMOVED] CC=gcc CFLAGS=-fPIC -fstack-protector -g -O3 AR=/usr/bin/ar ARFLAGS=cr CXX=g++ CXXFLAGS=-fstack-protector -g -O3 -fPIC F77=gfortran FFLAGS=-fPIC -ffree-line-length-0 -g -O3 FC=gfortran FCFLAGS=-fPIC -ffree-line-length-0 -g -O3 --enable-shared --with-device=ch3:sock --with-pm=hydra --enable-g=meminit

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 2013-05-24
    • 2016-06-10
    • 2018-08-27
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多