mtrace for fortran program

Dav*_*ide 5 c gcc fortran memory-leaks mtrace

我正试图用来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();
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我编译:

gfortran -g example.c leaky.f90
Run Code Online (Sandbox Code Playgroud)

然后我跑:

export MALLOC_TRACE=`pwd`/raw.txt; ./a.out
Run Code Online (Sandbox Code Playgroud)

然后我解析raw.txt mtrace输出:

mtrace a.out raw.txt
Run Code Online (Sandbox Code Playgroud)

得到:

没有内存泄漏.

有什么我做错了,或者我能做些什么来mtrace找到泄漏的fortran内存分配?我猜gfortran正在使用一个不同的malloc调用,它mtrace不会跟踪......实际上,正如我上面写的那样,如果我写一个可以调用(包装)mtrace()和的函数的fortran,我会得到相同的结果muntrace().

EDITED:我考虑了其他选项(包括一些尚未提及的选项),但是正在调试的实际代码在P6/AIX上运行,因此Valgrind"只是"不方便(它需要在不同的机器上运行),而Forcheck将是不方便(它需要在不同的机器上运行)和昂贵的(~3k $).如果有效的话,目前mtrace将是最好的解决方案.

再次编辑:我的猜测

我猜gfortran正在使用一个不同的malloc调用,它mtrace不会跟踪......

是对的.查看可执行文件(使用nmreadelf)没有任何malloc()调用,但有些_gfortran_allocate_array- 可能会调用malloc).还有其他想法吗?

再次编辑:我发布了答案,但我无法接受(请访问http://stackoverflow.uservoice.com/pages/general/suggestions/39426并请求该功能,它确实需要 - 无需获得声望)

Dav*_*ide 1

Steve Kargl 给出了答案,简单来说就是 mtrace 没有发现任何泄漏,因为如果编译器符合标准,则不会有任何泄漏:请参阅http://gcc.gnu.org/ml/fortran/2008-11 /msg00163.html了解详细信息。

事实上,我不是一个伟大的 Fortran 专家(我主要是 C/C++/java 人),而且我还使用了另一个编译器,它在这种情况下确实会泄漏(我没有提到这一点是为了让问题更容易)。因此我错误地认为 gfortran 也存在泄漏,但事实并非如此(我检查了 top)