我试图用mtrace
检测Fortran程序中的内存泄漏。我正在使用gfortran编译器。请参阅维基百科条目以获取mtrace的(工作)C示例:我尝试了两种方法,即将mtrace()和muntrace()包装在Fortran程序中并调用它们,并创建一个C程序直接调用mtrace()和muntrace(),除此之外的Fortran代码泄漏。 这两种方法都无法检测到内存泄漏,但在这里我仅仅介绍后者。命令mtrace的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
我编译:
export MALLOC_TRACE=`pwd`/raw.txt; ./a.out
:
gfortran -g example.c leaky.f90
然后我跑0
然后我解析raw.txt
mtrace
输出用:
mtrace a.out raw.txt
,并得到:
没有内存泄漏。
有什么我做错了,或者是我可以做的,让mtrace
找到漏水的FORTRAN内存分配?我猜gfortran正在使用不同的malloc
调用,mtrace
不跟踪... 事实上,正如我上面写的我得到同样的结果,如果我写一个FORTRAN主这将调用(包装)mtrace()
和muntrace()
。
编辑:我认为其他选项(包括一些尚未在此提及的),但实际正在调试的代码在P6/AIX上运行,所以Valgrind会“不便”(它需要在不同的机器上运行),而Forcheck会很不方便(它需要在不同的机器上运行)和昂贵的(〜3k $)。目前mtrace是最好的解决方案,如果它工作的话。
再次编辑: 我猜
我猜gfortran正在使用不同的
malloc
调用,mtrace
不跟踪...
是正确的。展望可执行文件(或者与nm
或readelf
)没有任何malloc()
电话,但_gfortran_allocate_array
的 - 这可能会调用malloc)。任何其他想法?
再次编辑: 我张贴的答案,但我不能接受它(去http://stackoverflow.uservoice.com/pages/general/suggestions/39426和要求的功能,它真正需要的 - 没有信誉收获希望)