valgrind reports "definitely lost" memory for very simple codes

Tobias Burnus burnus@net-b.de
Fri May 8 05:57:00 GMT 2015


Hi Damian, hi all,

Am 08.05.2015 um 07:04 schrieb Steve Kargl:
> On Thu, May 07, 2015 at 08:49:53PM -0700, Damian Rouson wrote:
>> Below are several very short programs in which valgrind reports memory
>> leaks.
>>
>> $ cat intrinsic-variable.f90
>>
>> real, allocatable :: x
>> x=0.
>> end
> Valgrind is broken here.  As the program is exiting,
> there is no need for the program to deallocate the
> memory.  On termination, the OS will reclaim the
> memory.

While true, it can be a quality of implementation to free the memory to 
better detect memory leaks elsewhere.

However, there is another reason gfortran no longer deallocates 
allocatables: Fortran 2008 states that variables in the main program 
automatically have the SAVE attribute. Thus, the compiler is not 
permitted to finalize finablizable variables. It could still deallocate 
them (as that is not really detectable in the user code) but it is 
difficult to draw the line. Thus, gfortran simply doesn't 
deallocate/finalize them at all.

If you want the compiler to automatically deallocate those variables, 
put your main program into a BLOCK, e.g.:

program main
   BLOCK
     real, allocatable :: x
     x=0.
   END BLOCK
end program main


Cheers,

Tobias



More information about the Fortran mailing list