I don't know how far ifort's support for type-bound final bindings goes right now, but here is some strange behaviour:
module m implicit none type :: derived_type ! allocatable component causes rerunning the finalizer integer, allocatable :: alloc_comp ! value 99 indicates that the finalizer already went over this integer :: finalized = 0 ! identification character :: ID = '?' contains final :: finalize end type contains ! finalizer subroutine finalize(arg) type(derived_type) :: arg print *, 'run finalizer on ID = ', arg%ID, ':' if (arg%finalized == 99) then print *, '...finalizing an already finalized object!' end if arg%finalized = 99 end subroutine ! main subroutine test_finalizer() type(derived_type), allocatable :: my_derived_alloc ! (1) type(derived_type) :: my_derived ! (2) ! mark local variable that sould be finalized my_derived%ID = 'X' ! additional code and allocations involving both variables ! does not change behaviour (as far as I tested it) end subroutine end module program p use m implicit none call test_finalizer() end program
- comment out (1): single finalization on local variable [OK]
- comment out (2): no finalization (no allocation) [OK]
- interchanging lines (1) and (2): single run of finalizer [STRANGE...]
- both uncommented: double finalization on loval variable [FAIL?]
- Is there ever a (legal) situation where multiple finalizations of the same object might happen?
- Are there any situations where finalization nessecary to deallocate derived-type allocatable components?
(Or is that taken care of in in every possible situation automatically?)
Thank you for advice and comments,
PS: my ifort version is 14.0.0