https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60913

Ian Harvey <ian_harvey at bigpond dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ian_harvey at bigpond dot com

--- Comment #10 from Ian Harvey <ian_harvey at bigpond dot com> ---
The issue appears to be that code generated by the compiler does not deallocate
the polymorphic allocatable function result when the innermost executable
construct containing the function reference terminates (F2008 6.7.3.2p5).  This
only appears to be the case for when the function result is polymorphic.

Clarifying questions here and elsewhere - this is something that only the
compiler can arrange (it must be "automatic"), as the allocatable
characteristic of the function result is only accessible to the compiler in the
calling scope.  The user does not need to arrange anything - there is nothing
they can arrange.  The presence or absence of a finalizer is not relevant - a
finalizer cannot explicitly deallocate the object that it is finalizing, it can
only explicitly deallocate allocatable sub-objects of the object being
finalized.  The type declaration statements mentioned in #8 are not
particularly relevant, the issue is with the handling of function references.

This may be a simpler example:

MODULE m
  IMPLICIT NONE

  TYPE :: t
    INTEGER :: comp
  END TYPE t
CONTAINS
  FUNCTION f(i)
    INTEGER, INTENT(IN) :: i
    CLASS(t), ALLOCATABLE :: f
    ALLOCATE(f)
    f%comp = i
  END FUNCTION f

  SUBROUTINE proc(arg)
    CLASS(t), INTENT(IN) :: arg
    PRINT *, arg%comp
  END SUBROUTINE proc
END MODULE m

PROGRAM p
  USE m
  IMPLICIT NONE
  INTEGER :: i
  DO i = 1, 100
    CALL proc(f(i))
    ! The function result should be deallocated after 
    ! execution of the above statement completes.
  END DO
END PROGRAM p

~~~

$ gfortran -g -v 2017-01-28\ alloc2.f90 && valgrind --leak-check=full ./a.out
Driving: gfortran -g -v 2017-01-28 alloc2.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/7.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: .././src/configure --prefix=/home/MEGMS2/ian/usr/gcc-7.0.1
--enable-languages=c,c++,fortran,lto --enable-libgomp --enable-checking=release
Thread model: posix
gcc version 7.0.1 20170120 (experimental) (GCC)
COLLECT_GCC_OPTIONS='-g' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/7.0.1/f951
2017-01-28 alloc2.f90 -quiet -dumpbase 2017-01-28 alloc2.f90 -mtune=generic
-march=x86-64 -auxbase 2017-01-28 alloc2 -g -version -fintrinsic-modules-path
/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/finclude -o
/tmp/ccRZA3YY.s
GNU Fortran (GCC) version 7.0.1 20170120 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 7.0.1 20170120 (experimental), GMP version
6.1.1, MPFR version 3.1.5, MPC version 1.0.2, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU Fortran2008 (GCC) version 7.0.1 20170120 (experimental)
(x86_64-pc-linux-gnu)
        compiled by GNU C version 7.0.1 20170120 (experimental), GMP version
6.1.1, MPFR version 3.1.5, MPC version 1.0.2, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-g' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccXmIutE.o /tmp/ccRZA3YY.s
GNU assembler version 2.26.1 (x86_64-redhat-linux) using BFD version version
2.26.1-1.fc25
Reading specs from
/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/../../../../lib64/libgfortran.spec
rename spec lib to liborig
COLLECT_GCC_OPTIONS='-g' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
COMPILER_PATH=/home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/7.0.1/:/home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/7.0.1/:/home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/:/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/:/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/:/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-g' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/7.0.1/collect2
-plugin
/home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/7.0.1/liblto_plugin.so
-plugin-opt=/home/MEGMS2/ian/usr/gcc-7.0.1/libexec/gcc/x86_64-pc-linux-gnu/7.0.1/lto-wrapper
-plugin-opt=-fresolution=/tmp/ccMep7Yj.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lquadmath
-plugin-opt=-pass-through=-lm -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
/lib/../lib64/crt1.o /lib/../lib64/crti.o
/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/crtbegin.o
-L/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1
-L/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/../../../../lib64
-L/lib/../lib64 -L/usr/lib/../lib64
-L/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/../../..
/tmp/ccXmIutE.o -lgfortran -lm -lgcc_s -lgcc -lquadmath -lm -lgcc_s -lgcc -lc
-lgcc_s -lgcc
/home/MEGMS2/ian/usr/gcc-7.0.1/lib/gcc/x86_64-pc-linux-gnu/7.0.1/crtend.o
/lib/../lib64/crtn.o
COLLECT_GCC_OPTIONS='-g' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
==435== Memcheck, a memory error detector
==435== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==435== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==435== Command: ./a.out
==435==
           1
...
         100
==435==
==435== HEAP SUMMARY:
==435==     in use at exit: 400 bytes in 100 blocks
==435==   total heap usage: 119 allocs, 19 frees, 12,408 bytes allocated
==435==
==435== 400 bytes in 100 blocks are definitely lost in loss record 1 of 1
==435==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==435==    by 0x400A20: __m_MOD_f (2017-01-28 alloc2.f90:11)
==435==    by 0x400A7D: MAIN__ (2017-01-28 alloc2.f90:26)
==435==    by 0x400AD4: main (2017-01-28 alloc2.f90:22)
==435==
==435== LEAK SUMMARY:
==435==    definitely lost: 400 bytes in 100 blocks
==435==    indirectly lost: 0 bytes in 0 blocks
==435==      possibly lost: 0 bytes in 0 blocks
==435==    still reachable: 0 bytes in 0 blocks
==435==         suppressed: 0 bytes in 0 blocks
==435==
==435== For counts of detected and suppressed errors, rerun with: -v
==435== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Reply via email to