https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107489
Bug ID: 107489 Summary: Runtime segfault in finalization routine of derived type with allocatable components Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: federico.perini at gmail dot com Target Milestone: --- I'm getting the same issue on a recursive tree structure. I see a segfault in the default finalizer (I think it gets called when a recursive type is returned as a function result). I get the segfault on the default finalization routine with all gfortran versions, 7.1 to 12.1. This is also potentially linked to bug 106606 (in this case, the class is not polymorphic). Test program (also see on godbolt at: https://godbolt.org/z/PxYGhM8j9) module t type :: tree type(tree), allocatable :: node end type tree type :: container type(tree) :: root end type container contains type(container) function do_something() result(c) allocate(c%root%node) allocate(c%root%node%node) allocate(c%root%node%node%node) end function do_something end module t program test_function_output use t call create_and_finalize() contains subroutine create_and_finalize() type(container) :: c1,c2 c1 = do_something() c2 = c1 print *, 'allocated? 0 ',allocated(c1%root%node) print *, 'allocated? 1 ',allocated(c1%root%node%node) print *, 'allocated? 2 ',allocated(c1%root%node%node%node) end subroutine create_and_finalize end program test_function_output Thanks, Federic