I am trying to understand how finalizers work in Fortran.
I have written a minimal test program containing a derived type with just a final procedure and an allocatable component.
It appears that the finalizer is called more that once, and I don't understand why.
I noticed this behaviour only if the derived type contains an allocatable component, and if more than one object is instantiated from that class.
Here is the code :
module finalizer_module implicit none type :: obj real,allocatable :: var contains final :: clean end type contains subroutine clean(this) type(obj) :: this write(*,*) "in finalizer" end subroutine clean end module finalizer_module ! program test_finalizer use :: finalizer_module call sub contains subroutine sub type(obj) :: f1,f2 end subroutine sub end program test_finalizer
And here the output :
in finalizer in finalizer in finalizer
I think I should have only two "in finalizer", as I have two objects (f1,f2) that are destroyed at "end subroutine sub"?
$ ifort --version ifort (IFORT) 14.0.2 20140120