fortran - Does the finalization routine need to be elemental in order to be called on the elements of allocatable array that goes out of scope? -
if have allocatable array of finalizable derived type, finalizer called on every individual element when array goes out of scope?
here small code example illustrates question:
module leakytypemodule implicit none private type, public :: leakytype real, pointer :: dontleakme(:) => null() contains procedure :: new final :: finalizer end type contains subroutine new(self, n) class(leakytype), intent(out) :: self integer , intent(in) :: n allocate(self%dontleakme(n)) self%dontleakme = 42.0 end subroutine subroutine finalizer(self) type(leakytype), intent(inout) :: self if (associated(self%dontleakme)) deallocate(self%dontleakme) end subroutine end module program leak use leakytypemodule implicit none type(leakytype), allocatable :: arr(:) allocate(arr(1)) call arr(1)%new(1000) deallocate(arr) end program
note program leaks dontleakme
array allocated in new()
method of leakytype
. @ first bit surprising me but, discovered problem can fixed declaring finalizer elemental
. both gfortran , ifort behave in same way, assume behaviour following fortran 2003 standard.
can confirm this? honest have hard time time understanding standard says on particular point.
right can't see use in not declaring finalizers elemental. have application i'm overlooking?
the rules determining whether final procedure invoked, , final procedure invoked, same resolution of generic procedures in terms of rank matching requirements.
noting question tagged fortran 2003...
elemental procedures in fortran 2003 , prior have pure. if finalizer needs incompatible pure attribute (which reasonably common) finalizer cannot elemental, , need write rank specific variants.
fortran 2008 introduces concept of impure elemental, quite handy writing finalizers.
Comments
Post a Comment