https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89890
Bug ID: 89890
Summary: Memory leak from a function returning a subtype
Product: gcc
Version: 8.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: andrew at fluidgravity dot co.uk
Target Milestone: ---
I get a memory leak from the code below. The leak does not occur with either
the Intel or PGI Fortran compilers.
The leak goes away if I change the return type of function 'new' to
"CLASS(subtype), ALLOCATABLE".
> gfortran-8 --version
GNU Fortran (SUSE Linux) 8.2.1 20180831 [gcc-8-branch revision 264010]
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> gfortran-8 -g -O0 -std=f2008 code.f90
> valgrind --tool=memcheck --leak-check=yes --show-leak-kinds=definite ./a.out
==25304== Memcheck, a memory error detector
==25304== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25304== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==25304== Command: ./a.out
==25304==
==25304==
==25304== HEAP SUMMARY:
==25304== in use at exit: 12 bytes in 2 blocks
==25304== total heap usage: 27 allocs, 25 frees, 13,553 bytes allocated
==25304==
==25304== 12 (8 direct, 4 indirect) bytes in 1 blocks are definitely lost in
loss record 2 of 2
==25304== at 0x4C2E01F: malloc (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==25304== by 0x400CAB: __m_MOD_new (code.f90:14)
==25304== by 0x400DA0: MAIN__ (code.f90:28)
==25304== by 0x400F10: main (code.f90:25)
==25304==
==25304== LEAK SUMMARY:
==25304== definitely lost: 8 bytes in 1 blocks
==25304== indirectly lost: 4 bytes in 1 blocks
==25304== possibly lost: 0 bytes in 0 blocks
==25304== still reachable: 0 bytes in 0 blocks
==25304== suppressed: 0 bytes in 0 blocks
==25304==
==25304== For counts of detected and suppressed errors, rerun with: -v
==25304== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
code.f90:
MODULE m
IMPLICIT NONE
TYPE, ABSTRACT, PUBLIC :: base
END TYPE
TYPE, EXTENDS(base), PUBLIC :: subtype
INTEGER, ALLOCATABLE :: x
CONTAINS
FINAL :: subtype_final
END TYPE
CONTAINS
FUNCTION new(this)
INTEGER :: this
CLASS(base), ALLOCATABLE :: new
ALLOCATE(subtype::new)
SELECT TYPE ( new )
CLASS IS ( subtype )
ALLOCATE(new%x, SOURCE=this)
END SELECT
END
SUBROUTINE subtype_final(this)
TYPE(subtype) :: this
IF ( ALLOCATED(this%x) ) DEALLOCATE(this%x)
END
END
USE m
IMPLICIT NONE
CLASS(base), ALLOCATABLE :: w
ALLOCATE(w, SOURCE=new(0))
DEALLOCATE(w)
END