Fwd: Valgrind (and inspxe-cl) reports lost memory using gfortran 4.8.2 with allocatables

Paul van Delst paul.vandelst@noaa.gov
Tue Oct 7 13:44:00 GMT 2014


Hello,

Sorry if people see this twice. I meant to post here but my muscle 
memory pointed me to clf. Oops.

cheers,

paulv


-------- Original Message --------
Subject: Valgrind (and inspxe-cl) reports lost memory using gfortran 
4.8.2 with allocatables
Date: Tue, 07 Oct 2014 09:04:29 -0400
From: Paul van Delst <paul.vandelst@noaa.gov>
Organization: Aioe.org NNTP Server
Newsgroups: comp.lang.fortran

Hello,

I'm trying to figure out if I'm doing something wrong, or if I simply
need to upgrade my version of gfortran.

My test program is shown below. I define a structure containing
allocatables along with a subroutine (allocate_mytype) out of which I
pass an array of that type, with the dummy itself being allocatable.

Note in the main program there is a loop over the call to allocate_mytype().

-----%<-----
module testmodule
   implicit none
   private
   public :: mytype_type
   public :: allocate_mytype
   type :: mytype_type
     integer, allocatable :: i(:)
   end type mytype_type

contains

   elemental subroutine create_mytype(mytype,n)
     type(mytype_type), intent(out) :: mytype
     integer, intent(in) :: n
     allocate(mytype%i(n))
     mytype%i = 0
   end subroutine create_mytype

   subroutine allocate_mytype(array)
     type(mytype_type), allocatable, intent(out) :: array(:)
     allocate(array(20))
     call create_mytype(array,5)
   end subroutine allocate_mytype
end module testmodule

program testprogram
   use testmodule
   implicit none
   integer :: n
   type(mytype_type), allocatable :: array(:)

   do n = 1, 3
     print *, allocated(array)
     call allocate_mytype(array)
   end do

end program testprogram
-----%<-----


When I compile and run the program through valgrind I get the following:


-----%<-----
$ gfortran --version
GNU Fortran (GCC) 4.8.2 20130801 (prerelease)

$ gfortran -g test_allocarg.f90

$ valgrind --leak-check=full ./a.out
==14695== Memcheck, a memory error detector
==14695== Command: ./a.out
==14695==
  F
  T
  T
==14695==
==14695== HEAP SUMMARY:
==14695==     in use at exit: 800 bytes in 40 blocks
==14695==   total heap usage: 80 allocs, 40 frees, 7,791 bytes allocated
==14695==
==14695== 800 bytes in 40 blocks are definitely lost in loss record 1 of 1
==14695==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
==14695==    by 0x400E6A: __testmodule_MOD_create_mytype (in ...)
==14695==    by 0x400CA4: __testmodule_MOD_allocate_mytype (in ...)
==14695==    by 0x400FD4: MAIN__ (in ...)
==14695==    by 0x4010CD: main (in ...)
==14695==
==14695== LEAK SUMMARY:
==14695==    definitely lost: 800 bytes in 40 blocks
==14695==    indirectly lost: 0 bytes in 0 blocks
==14695==      possibly lost: 0 bytes in 0 blocks
==14695==    still reachable: 0 bytes in 0 blocks
==14695==         suppressed: 0 bytes in 0 blocks
==14695==
==14695== For counts of detected and suppressed errors, rerun with: -v
==14695== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
-----%<-----


When I do the same but using ifort and inspxe-cl, everything appears fine:


-----%<-----
$ ifort test_allocarg.f90

$ inspxe-cl -collect=mi1 ./a.out
Used suppression file(s): []
  F
  T
  T
0 new problem(s) found

$ inspxe-cl -report=problems
Used suppression file(s): []
-----%<-----


I then decided to mix the two - compiling with gfortran but running it
through inspxe (dont know if that's kosher...). This is what I got:


-----%<-----
$ gfortran -g test_allocarg.f90

$ inspxe-cl -collect=mi1 ./a.out
Used suppression file(s): []
  F
  T
  T
1 new problem(s) found
     1 Memory leak problem(s) detected

$ inspxe-cl -report=problems
Used suppression file(s): []
New Problem P1: Error: Memory leak
test_allocarg.f90(15): Error X3: P1: Memory leak: Allocation site:
Function create_mytype: Module a.out
-----%<-----


If I call the allocate_mytype() procedure just ONCE, there is no
reported memory leak using valgrind with gfortran. It's calling that
procedure when the actual argument is already allocated when the problem
surfaces.

So, it would appear that when allocating an array of structures that
contain allocatable components in a procedure where the dummy argument
has the allocatable attribute, the deallocation that should occur due to
the INTENT(OUT) is not happening.

Is this a case of me misunderstanding the standard with respect to
ALLOCATABLE and INTENT(OUT) dummy args, or a bug in this particular
version of gfortran, or...?

Any help appreciated.

Thanks,

paulv






More information about the Fortran mailing list