【发布时间】:2010-09-20 14:03:07
【问题描述】:
我正在尝试使用mtrace 来检测fortran 程序中的内存泄漏。我正在使用 gfortran 编译器。有关 mtrace 的(工作)C 示例,请参阅维基百科条目:http://en.wikipedia.org/wiki/Mtrace
我尝试了两种方法,即包装 mtrace() 和 muntrace() 并从 fortran 程序中调用它们,以及创建一个直接调用 mtrace() 和 muntrace() 的 C 程序,除了泄漏的 fortran 代码介于两者之间。 这两种方法都无法检测到内存泄漏,但这里我只介绍后者。
example.c
#include <stdlib.h>
#include <mcheck.h>
extern void leaky_(); // this might be different on your system
// if it doesn't work, try to run:
// 1) gfortran leaky.f90 -c
// 2) nm leaky.o
// and then change this declaration and its use below
void main() {
mtrace();
leaky_();
muntrace();
}
leaky.f90
subroutine leaky()
real, allocatable, dimension(:) :: tmp
integer :: error
allocate (tmp(10), stat=error)
if (error /= 0) then
print*, "subroutine leaky could not allocate space for array tmp"
endif
tmp = 1
!of course the actual code makes more...
print*, ' subroutine leaky run '
return
end subroutine leaky
我编译:
gfortran -g example.c leaky.f90
然后我运行:
export MALLOC_TRACE=`pwd`/raw.txt; ./a.out
然后我解析raw.txt mtrace 输出:
mtrace a.out raw.txt
然后得到:
没有内存泄漏。
我做错了什么,或者我可以做些什么让mtrace 找到泄漏的fortran 内存分配?我猜 gfortran 正在使用不同的malloc 调用,mtrace 没有跟踪...
事实上,正如我在上面所写的,如果我编写一个调用(包装的)mtrace() 和 muntrace() 的 fortran 主程序,我会得到相同的结果。
已编辑:我考虑了其他选项(包括此处尚未提及的一些选项),但正在调试的实际代码在 P6/AIX 上运行,因此 Valgrind 将“只是”不方便(它需要在不同的机器上运行),而Forcheck 会很不方便(它需要在不同的机器上运行)并且价格昂贵(~ 3k$)。如果可行的话,目前 mtrace 将是最好的解决方案。
再次编辑: 我的猜测
我猜 gfortran 正在使用不同的
malloc调用,mtrace没有跟踪...
是正确的。查看可执行文件(使用nm 或readelf)没有任何malloc() 调用,但_gfortran_allocate_array 调用-可能会调用malloc)。还有其他想法吗?
再次编辑: 我发布了答案,但我无法接受(转至 http://stackoverflow.uservoice.com/pages/general/suggestions/39426 并请求该功能,它真的很需要 - 不希望获得声誉)
【问题讨论】:
标签: c gcc memory-leaks fortran mtrace