In the documentation of ifort it says
If the entity being finalized is an array, each finalizable component of each element of that array is finalized separately.
and there is no word about elemental
being necessary.
But if I have a vector of finalizable components, there are memory leaks if the final
procedure is not declares as elemental. (The memory leaks being reported by valgrind
.)
module vector_mod
implicit none(type, external)
private
public :: Vector_t
type :: Vector_t
real, pointer :: X(:) => null()
contains
procedure :: init
final :: finalize
end type
contains
subroutine init(this, n, val)
class(Vector_t), intent(out) :: this
integer, intent(in) :: n
real, intent(in) :: val
allocate(this%X(n), source=val)
end subroutine
! If this elemental is missing I get memory leaks in
! - gfortran 7.5.0
! - ifort 19.1.1.217
elemental subroutine finalize(this)
type(Vector_t), intent(inout) :: this
if (associated(this%X)) deallocate(this%X)
end subroutine
end module
program test_final_intentout
use vector_mod, only: Vector_t
implicit none(type, external)
block
type(Vector_t), allocatable :: vectors(:)
integer :: i
allocate(vectors(3))
do i = 1, size(vectors)
call vectors(i)%init(i, 1.)
end do
end block
end program