https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104697
Bug ID: 104697 Summary: Memory leak with ALLOCATABLE COMPONENTS and SOURCE= expression and POINTER Product: gcc Version: 12.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: burnus at gcc dot gnu.org Target Milestone: --- The following leaks twice. Once for the SOURCE= expression: allocate(var%D, source=reshape([t2(1), t2(2), t2(3), t2(4)], [2,2])) This seems to create temporaries which do not get deallocated. Secondly, the deep freeing does not seem to work for: deallocate (var%D) This can be fixed by using deallocate (var%D(1,1)%x, var%D(1,2)%x, var%D(2,1)%x, var%D(2,2)%x) deallocate (var%D) instead. Compiling with -fsanitize=address shows the 8 memory leaks - or 4 memory leaks when commenting in the additional deallocate. implicit none type t2 integer, allocatable :: x end type t2 type t type(t2), pointer :: D(:,:) => null() end type t type(t) :: var allocate(var%D, source=reshape([t2(1), t2(2), t2(3), t2(4)], [2,2])) if (any (shape(var%D) /= [2,2])) stop 1 if (var%D(1,1)%x /= 1 .or. var%D(1,2)%x /= 3 .or. & var%D(2,1)%x /= 2 .or. var%D(2,2)%x /= 4) stop 2 ! deallocate (var%D(1,1)%x, var%D(1,2)%x, var%D(2,1)%x, var%D(2,2)%x) deallocate (var%D) end