Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Final subroutine not invoked

Bálint_A_
Beginner
272 Views

Dear All,

It seems, that intel 16.0.3 (I do not have any more recent version currently) shows non-standard behavior when doing finalization: Given a derived type without a final subroutine, but with an allocatable component with final subroutine, the final subroutine of the allocatable component is not invoked, when a given instance of the derived type is finalized.

The example below demonstrates the issue. The destructor of MyType is never invoked, although the allocatable array, where it is stored, should be finalized, when the instance of MyTypeWrapper is goes out of scope (is deallocated).

Best regards,

Bálint

module testmod
  implicit none

  type :: MyType
    integer :: data = 0
  contains
    final :: destruct
  end type MyType

  type :: MyTypeWrapper
    type(MyType), allocatable :: container(:)
  end type MyTypeWrapper

contains

  subroutine destruct(this)
    type(MyType), intent(inout) :: this

    print *, 'Destroying:', this%data
    this%data = 0

  end subroutine destruct

end module testmod


program testifort
  use testmod

  type(MyTypeWrapper), allocatable :: wrapper

  allocate(wrapper)
  allocate(wrapper%container(1))
  wrapper%container(1)%data = 1
  deallocate(wrapper)
  allocate(wrapper)
  allocate(wrapper%container(1))
  wrapper%container(1)%data = 2
  deallocate(wrapper)
  print *, 'DONE'
  
end program testifort

 

0 Kudos
3 Replies
Bálint_A_
Beginner
272 Views

For some reasons I can not edit the post above. The code contains a typo, as the destructor should be defined for rank one objects. The correct version is, which is not treated by the compiler as it should follows:

module testmod
  implicit none

  type :: MyType
    integer :: data = 0
  contains
    final :: destruct
  end type MyType

  type :: MyTypeWrapper
    type(MyType), allocatable :: container(:)
  end type MyTypeWrapper

contains

  subroutine destruct(this)
    type(MyType), intent(inout) :: this(:)

    print *, 'Destroying:', this%data
    this%data = 0

  end subroutine destruct

end module testmod


program testifort
  use testmod

  type(MyTypeWrapper), allocatable :: wrapper

  allocate(wrapper)
  allocate(wrapper%container(1))
  wrapper%container(1)%data = 1
  deallocate(wrapper)
  allocate(wrapper)
  allocate(wrapper%container(1))
  wrapper%container(1)%data = 2
  deallocate(wrapper)
  print *, 'DONE'
  
end program testifort


 

0 Kudos
Kevin_D_Intel
Employee
272 Views

Thank you for reporting this. I'll investigate.

0 Kudos
Kevin_D_Intel
Employee
272 Views

ifort handles the polymorphic case CLASS(MyTypeWrapper) successfully but not the case you presented. I reported your case to Development.

(Internal tracking id: DPD200414065)

0 Kudos
Reply