Memory leak with automatic array dummy argument with derived type array constructor actual argument

Damian Rouson damian@sourceryinstitute.org
Sat May 9 01:22:00 GMT 2015


GFotran Developers,

I just submitted the code below as Bug 66082.  It demonstrates a memory leak generated by an automatic array dummy argument with a corresponding actual argument that is a derived type array constructor with an allocatable component.  Changing the dummy argument declaration "foo(n)" to an assumed-shape array "foo(:)" eliminates the leak.  As recommended by Tobias in a separate thread, a block construct wraps the main program's declaration and executable lines to ensure that implicit deallocations should occur.  Valgrind reports the same results for code compiled with 4.9.2, 5.1.0, and 6.0.0. 

Damian

$ cat reduced.f90 
  type foo_t
     real, allocatable :: bigarr
  end type 
  block
    type(foo_t) :: foo 
    allocate(foo%bigarr) 
    call foo_1d(1,[foo]) ! definitely lost
  end block
contains
  subroutine foo_1d(n,foo)
    integer n
    type(foo_t) :: foo(n)
  end subroutine 
end 
$ gfortran reduced.f90 
$ valgrind ./a.out
==7891== Memcheck, a memory error detector
==7891== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. 
==7891== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==7891== Command: ./a.out
==7891== 
==7891== 
==7891== HEAP SUMMARY:
==7891==     in use at exit: 4 bytes in 1 blocks
==7891==   total heap usage: 26 allocs, 25 frees, 12,662 bytes allocated
==7891== 
==7891== LEAK SUMMARY:
==7891==    definitely lost: 4 bytes in 1 blocks
==7891==    indirectly lost: 0 bytes in 0 blocks
==7891==      possibly lost: 0 bytes in 0 blocks
==7891==    still reachable: 0 bytes in 0 blocks
==7891==         suppressed: 0 bytes in 0 blocks
==7891== Rerun with --leak-check=full to see details of leaked memory
==7891== 
==7891== For counts of detected and suppressed errors, rerun with: -v
==7891== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
$ gfortran --version
GNU Fortran (GCC) 6.0.0 20150503 (experimental)


More information about the Fortran mailing list