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

            Bug ID: 114613
           Summary: ALLOCATE with SOURCE forgets to finalize temporary
                    instances
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vterzi1996 at gmail dot com
  Target Milestone: ---

Consider the following minimal example:
```
module lib
    private
    type,public::list
    contains
        final::list_final
    end type
    interface list
        procedure list_new
    end interface
contains
    function list_new()result(this)
        type(list)::this
        print*,'list_new'
    end
    subroutine list_final(this)
        type(list),intent(inout)::this
        print*,'list_final'
    end
end


program prog
    use lib
    type(list),allocatable::obj
    allocate(obj,source=list())  ! obj=list()
end
```

Only `list_new` is printed, since `list_final` is never called to finalize the
temporary `list()` instance after the `allocate` statement, what can lead to a
memory leak. This issue can be fixed by replacing `allocate(obj,source=list())`
with `obj=list()`.

A larger example was already discussed here:
https://stackoverflow.com/questions/78279925/right-way-to-allocate-type-instances-containing-pointers-in-fortran
and one of the experienced Stack Overflow users thinks that it is a bug of
gfortran.

Reply via email to