I'm having trouble when I pass a portion of an array of a derived type which has an allocatable component. A (probable) memory leaks seems to occur (unfortunatly valgrind is not yet available on mac 10.14/mojave, so I simply used a long do loop and looked the memory used with top).
Consider the following test program which reproduces my problem in a simpler context:
program test_leak
implicit none
type :: vint_t
integer, allocatable :: col(:)
end type vint_t
type(vint_t) :: row(4)
integer :: i, Ind(2)=[2,3]
do i = 1, 4
allocate(row(i)%col(i))
end do
row(1)%col(1) = 11
row(2)%col(1) = 21 ; row(2)%col(2) = 22
row(3)%col(1) = 31 ; row(3)%col(2) = 32 ; row(3)%col(3) = 33
row(4)%col(1) = 41 ; row(4)%col(2) = 42 ; row(4)%col(3) = 43 ; row(4)%col(4) = 44
do i = 1, 1000000
call do_nothing ( row(Ind) ) ! (version #A) with this version and with gfortran: memory grows with iter
!call do_nothing ( row(2:3) ) ! (version #B) but not with this version
if (mod(i,10000) == 0) then
print*,i ; read*
end if
end do
contains
subroutine do_nothing ( a )
type(vint_t), intent(in) :: a(:)
end subroutine do_nothing
end program test_leak
- The problem occurs with gfortran (8.2) and not with ifort (19) nor with nagfor (6.2) (with nagfor and ifort the used memory is stable during the iterations and ca 400 Kb, while it reaches ca 30 Mb with gfortran!).
- A final procedure doesn't solve the problem.
- The problem disapears if the allocatable member "col" is replaced by an explicit array (say col(4)) or if the version #B is used.
- The problem also occurs with an allocatable character component.
- If I print "a" in the subroutine "do_nothing" both versions give the correct result.
Does anyone have some insight on this issue? Of course I can design the called subroutines by passing the array Ind (do_nothing(row, Ind)) but that would be somewhat a pity.