Fortran finalization subroutine does not work when type is an array

33 Views Asked by At

Recently I found a serious problem that is in some cases finalization function does not work.

Description:

I defined a type, say TYPE(testtype):: T, in which many pointers and allocatable arrays being used. And there is a finalization subroutine, say final:: de. This function (de) works fine (I put write in it to check that). However, when I define an array TYPE(testtype),allocatable::T(:), after I finish using it, function de is not called when I deallocate(T). Here I put on the testing code. As one can check, subdo1() is working well( output "work"), while final in subdo2() is failed.

    module test1
        implicit none
        !--------------
        type testtype
          real(8)::x
        contains
          final::de
        end type
        !--------------
        contains
        subroutine de(self)
            type(testtype)::self
            write(*,*)"work"
        end subroutine
    end module

    subroutine subdo1()
        use test1
        implicit none
        class(testtype),pointer::t
        allocate(t)
        deallocate(t)
    end subroutine

    subroutine subdo2()
        use test1
        implicit none
        class(testtype),pointer::t(:)
        allocate(t(2))
        deallocate(t)
    end subroutine

    program test
        implicit none
        !call subdo1()
        call subdo2()
    end program

I tried both gfortran and ifort (ifort version 17.0.0)

0

There are 0 best solutions below