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

            Bug ID: 123868
           Summary: 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

  type :: vs
     character(len=1), allocatable :: s
  end type vs

  type :: ih
     type(vs), allocatable, dimension(:) :: hk
  end type ih

end module bugMod

program bugProg
  use bugMod

  block
    type(ih) :: c, d

    allocate(d%hk(1))
    allocate(d%hk(1)%s)
    d%hk(1)%s='z'
    c=d
    write (*,*) c%hk(1)%s,d%hk(1)%s

  end block

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 : (reconfigured) ../configure
--prefix=/carnegie/nobackup/users/abenson/upstream --disable-multilib
--enable-checking=release --enable-host-shared --with-pic
--enable-languages=c,c++,fortran,jit,lto : (reconfigured) ../configure
--prefix=/carnegie/nobackup/users/abenson/upstream --disable-multilib
--enable-checking=release --enable-host-shared --with-pic
--enable-languages=c,c++,fortran,jit,lto --no-create --no-recursion
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.0.1 20260123 (experimental) (GCC) 

> gfortran bug.F90 -g

> !va
valgrind --leak-check=full ./a.out 
==1452190== Memcheck, a memory error detector
==1452190== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==1452190== Using Valgrind-3.25.1 and LibVEX; rerun with -h for copyright info
==1452190== Command: ./a.out
==1452190== 
 zz
==1452190== 
==1452190== HEAP SUMMARY:
==1452190==     in use at exit: 8 bytes in 1 blocks
==1452190==   total heap usage: 22 allocs, 21 frees, 5,510 bytes allocated
==1452190== 
==1452190== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
==1452190==    at 0x484682F: malloc (vg_replace_malloc.c:446)
==1452190==    by 0x40233F: MAIN__ (bug.F90:21)
==1452190==    by 0x4027C5: main (bug.F90:14)
==1452190== 
==1452190== LEAK SUMMARY:
==1452190==    definitely lost: 8 bytes in 1 blocks
==1452190==    indirectly lost: 0 bytes in 0 blocks
==1452190==      possibly lost: 0 bytes in 0 blocks
==1452190==    still reachable: 0 bytes in 0 blocks
==1452190==         suppressed: 0 bytes in 0 blocks
==1452190== 
==1452190== For lists of detected and suppressed errors, rerun with: -s
==1452190== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


With ifort, no memory leak:

> ifort bug.F90 -g

> valgrind --leak-check=full ./a.out 
==1452207== Memcheck, a memory error detector
==1452207== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==1452207== Using Valgrind-3.25.1 and LibVEX; rerun with -h for copyright info
==1452207== Command: ./a.out
==1452207== 
 zz
==1452207== 
==1452207== HEAP SUMMARY:
==1452207==     in use at exit: 0 bytes in 0 blocks
==1452207==   total heap usage: 14 allocs, 14 frees, 12,849 bytes allocated
==1452207== 
==1452207== All heap blocks were freed -- no leaks are possible
==1452207== 
==1452207== For lists of detected and suppressed errors, rerun with: -s
==1452207== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)


The memory leak occurs only if both the `s` and `hk` components are
`allocatable`, and `hk` is not a scalar.

Reply via email to