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

            Bug ID: 123938
           Summary: [16-regession] Memory leak on assignment
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abensonca at gcc dot gnu.org
  Target Milestone: ---

The following causes a memory leak on assignment with gfortran 16:

module bugMod
  private
  public :: df1

  type, abstract :: t1
  end type t1

  type, extends(t1) :: t1g
     logical, allocatable :: i
  end type t1g

  type :: df0
     private
   contains
     procedure :: df0Assign
     generic :: assignment(=) => df0Assign
  end type df0

  type, extends(df0) :: df1
     private
     class(t1), allocatable :: c
  end type df1

  interface df1
     module procedure df1c
  end interface df1

contains

  subroutine df0Assign(self,from)
    implicit none
    class(df0), intent(out) :: self
    class(df0), intent(in   ) :: from
    return
  end subroutine df0Assign

  function df1c() result(self)
    type(df1) :: self 
    allocate(t1g :: self%c)
    select type (c => self%c)
    type is (t1g)
       allocate(c%i) ! this allocation is leaked
    end select
    return
  end function df1c

end module bugMod

program bugProg
  use :: bugMod
  type(df1) :: d
  d=df1()
end program bugProg


> gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/carnegie/nobackup/users/abenson/upstream/libexec/gcc/x86_64-pc-linux-gnu/16.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure
--prefix=/carnegie/nobackup/users/abenson/upstream --disable-multilib
--enable-checking=release --enable-host-shared --with-pic
--enable-languages=c,c++,fortran,jit,lto
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.0.1 20260130 (experimental) (GCC) 

> gfortran bug.F90 -g -fsanitize=leak
> ./a.out 

=================================================================
==2829789==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x147a3e0f0f4b in malloc
../../../../libsanitizer/lsan/lsan_interceptors.cpp:74
    #1 0x000000401e9b in __bugmod_MOD_df1c
/carnegie/scidata/users/abenson/Galacticus/galacticus_finalization/bug.F90:42
    #2 0x000000401fbb in bugprog
/carnegie/scidata/users/abenson/Galacticus/galacticus_finalization/bug.F90:52
    #3 0x000000402047 in main
/carnegie/scidata/users/abenson/Galacticus/galacticus_finalization/bug.F90:50
    #4 0x147a3da2a60f in __libc_start_call_main (/lib64/libc.so.6+0x2a60f)
(BuildId: e650335ac8463e9e58c04e07c6f36c5f826ed953)

SUMMARY: LeakSanitizer: 4 byte(s) leaked in 1 allocation(s).

With ifort, no leak is reported. gfortran-15.2 also reports no leak, so this
seems to be a regression.

Reply via email to