https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116679
Bug ID: 116679
Summary: Memory leak when creating derived type instance with
allocatable component within array expression
Product: gcc
Version: 14.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: baradi09 at gmail dot com
Target Milestone: ---
Issue:
GIVEN a derived type with an allocatable component
WHEN an instance of this type is created within an array expression
THEN the allocatable component is not deallocated when the instance goes out
of scope
Minimal working example:
program testprog
implicit none
type :: wrapper
integer, allocatable :: val
end type wrapper
block
type(wrapper) :: items(1)
items = [wrapper(1)]
end block
end program testprog
Compiling:
gfortran -Og -g3 -fsanitize=address memoryleak.f90
Obtained result:
When the executable finishes, AddressSanitizer reports unallocated memory
./a.out
=================================================================
==104059==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4 byte(s) in 1 object(s) allocated from:
#0 0x7f012e4b1cbc in malloc
../../../../libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x55717df5c307 in testprog
/home/aradi/ramdisk/BUILD/fortuno/memoryleak.f90:10
#2 0x7f012e11d14a in __libc_start_main_alias_1 (/lib64/libc.so.6+0x2a14a)
(BuildId: 77c77fee058b19c6f001cf2cb0371ce3b8341211)
#3 0x55717df5c14d in _start
(/media/aradi/ramdisk/BUILD/fortuno/a.out+0x114d)
SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s).
Expected result:
Memory should be properly deallocated at the end of the "block" construct.
Note: This is an extracted minimal working example of the issue reported for
the Fortuno unit testing system
(https://github.com/fortuno-repos/fortuno/issues/22)