I read on this tutorial that allocatable variables are automatically deallocated when they go out of scope. I wrote a trial program to test this, and I'm finding that valgrind says there is a memory leak. My trial program is:
program arrays
use, intrinsic :: iso_fortran_env, only: prec=>real64
implicit none
integer, allocatable :: array_1(:)
integer, allocatable :: array_2(:,:)
allocate(array_1(10))
allocate(array_2(10,10))
end program arrays
I compiled this program (arrays.f90) as follows (gfortran version "GNU Fortran (Ubuntu 5.5.0-12ubuntu1~16.04) 5.5.0 20171010")
gfortran -g -std=f2008 arrays.f90 -o arr
Then running valgrind (valgrind ./arr) I find
==13293== HEAP SUMMARY:
==13293== in use at exit: 440 bytes in 2 blocks
==13293== total heap usage: 21 allocs, 19 frees, 12,280 bytes allocated
==13293==
==13293== LEAK SUMMARY:
==13293== definitely lost: 440 bytes in 2 blocks
==13293== indirectly lost: 0 bytes in 0 blocks
==13293== possibly lost: 0 bytes in 0 blocks
==13293== still reachable: 0 bytes in 0 blocks
==13293== suppressed: 0 bytes in 0 blocks
==13293== Rerun with --leak-check=full to see details of leaked memory
I am confused about why valgrind is saying the two arrays are not being deallocated once the program ends.
The output of valgrid does not depend on the optimization level of the compiler I am using, in distinction to this question about memory leaks in fortran.