https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88551
Bug ID: 88551 Summary: passing a portion of an array of a derived type that contains an allocatable component Product: gcc Version: 8.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: hassani at unice dot fr Target Milestone: --- gcc -v Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1 Apple LLVM version 10.0.0 (clang-1000.11.45.5) Target: x86_64-apple-darwin18.2.0 gfortran --version GNU Fortran (Homebrew GCC 8.2.0) 8.2.0 ----------------------------------------------------------------------------------------- Dear all, It seems that passing a portion of an array of a DT which has an allocatable component to a subroutine can lead to memory leaks. Here is a small program that reproduces this situation: program test_leaks implicit none type :: vint_t integer, allocatable :: col(:) end type vint_t type(vint_t) :: row(4) integer :: i, Ind(2)=[3,2] 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) ) 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_leaks - The memory usage grows linearly with iterations when compiled on mac OS 10.14.2 with gfortran 8.2.0 - But the program works well (in the sense that a print in "do_nothing" shows the expected values) - The problem also occurs with an allocatable character component. - If "call do_nothing ( row(Ind) )" is replaced by "call do_nothing ( row(2:3) )" the issue goes away. - It also goes away if the allocatable component ("col") is replaced by a fixed sized one (e.g. col(4)) - The problem seems to be dependent of the OS. For example, it doesn't occur on linux (even with an old version of gfortran: 4.8.5). (For some additional details about this point, please see the answers to my question on stackoverflow (https://stackoverflow.com/questions/53839760/passing-a-portion-of-an-array-of-a-derived-type-that-contains-an-allocatable-com). Best Regards.