1

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
mcocdawc
  • 1,748
  • 1
  • 12
  • 21
  • Yes, I did have similar leaks using ifort which is why I dont use `final` no more... – jack Oct 15 '20 at 14:06
  • 1
    To provide a final subroutine that covers both scalars and arrays then it needs to be elemental. Without being elemental but taking a scalar dummy, you don't have a final subroutine for an array object. – francescalus Oct 15 '20 at 14:11
  • 2
    I'll try to find my old example where `final` was broken. I think it called that routine multiple times... it is definitely buggy, be warned! – jack Oct 15 '20 at 14:18
  • @francescalcus makes sense. But then the ifort documentation is wrong (or at least misleading) – mcocdawc Oct 16 '20 at 15:51
  • I think the ifort documentation is _correct_ if not _helpful_. That is, what happens is correct as it quotes the Fortran standard, but it fails to explain the background for what that means. This makes your question here a reasonable one (and I'd answer if there wasn't the other question covering it), but I'll say that the ifort (most compiler) documentation is a truly terrible way to learn how to use the Fortran language compared with a proper guide. (Sorry for the late reply: your @ had a misspelling.) – francescalus Oct 21 '20 at 23:23

0 Answers0