This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: mtrace for a fortran program


On Fri, Nov 14, 2008 at 03:24:46PM -0700, Davide Del Vento wrote:
> Carlos O'Donell wrote:
> >> >> I'm trying to use mtrace to detect memory leaks in a fortran program.
> >> >> I'm using the gfortran compiler.
> >> >> Unfortunately it does not work. I posted more details (including an
> >> >> example program) in this forum:
> >> >> http://stackoverflow.com/questions/260192/mtrace-for-a-fortran-program
> >> >>
> > > Does your gfortran program actually call malloc?
> 
> I guess that _gfortran_allocate_array will call malloc, but of course I
> do not know for sure (does anyone from the fortran list know?).
> If this is the case, would it be possible to configure mtrace in order
> to trace this indirect malloc calls?
> Thanks again,
> Davide Del Vento, Consulting Services Software Engineer
> NCAR Computational & Information Services Laboratory
> http://www.cisl.ucar.edu/hss/csg/
> 
> PS:
> Unfortunately it seems that this fortran mailing list does not allow
> crossposting. If you reply, please send a (separate, but identical)
> message in reply to this one. Thanks
> 
> http://sources.redhat.com/ml/libc-help/2008-11/msg00016.html
> 

The code you posted in 

http://stackoverflow.com/questions/260192/mtrace-for-a-fortran-program

does not leak memory.  If the compiler, in our case gfortran, conforms
to the Fortran 95 standard, it will not leak memory.

Here's your code without the IO statements, which makes the output of
-fdump-tree-original easier to read.

  subroutine leaky()
  real, allocatable, dimension(:) :: tmp
  allocate (tmp(10))
  tmp = 1
  end subroutine leaky

% gfortran43 -c -fdump-tree-original mem.f90
% cat mem.f90.003t.original
leaky ()
{
  struct array1_real(kind=4) tmp;

  tmp.data = 0B;
  {
    void * D.904;

    tmp.dtype = 281;
    tmp.dim[0].lbound = 1;
    tmp.dim[0].ubound = 10;
    tmp.dim[0].stride = 1;
    if (tmp.data == 0B)
      {
        {
          void * D.905;

          D.905 = __builtin_malloc (40);
          if (D.905 == 0B)
            {
              _gfortran_os_error (&"Out of memory"[1]{lb: 1 sz: 1});
            }
          D.904 = D.905;
        }
      }
    else
      {
        _gfortran_runtime_error (&"Attempting to allocate already allocated array"[1]{lb: 1 sz: 1});
      }
    tmp.data = D.904;
    tmp.offset = -1;
  }
  {
    integer(kind=8) D.909;
    integer(kind=8) D.908;
    integer(kind=8) D.907;
    real(kind=4)[0:] * D.906;

    D.906 = (real(kind=4)[0:] *) tmp.data;
    D.907 = tmp.offset;
    D.908 = tmp.dim[0].lbound;
    D.909 = tmp.dim[0].ubound;
    {
      integer(kind=8) D.911;
      integer(kind=8) S.0;

      D.911 = tmp.dim[0].stride;
      S.0 = D.908;
      while (1)
        {
          if (S.0 > tmp.dim[0].ubound) goto L.1;
          (*D.906)[S.0 * D.911 + D.907] = 1.0e+0;
          S.0 = S.0 + 1;
        }
      L.1:;
    }
  }
  if (tmp.data != 0B)
    {
      __builtin_free (tmp.data);
    }
  tmp.data = 0B;
}

-- 
Steve


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]