Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
12 Views

Final subroutine not invoked

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
Highlighted
Beginner
12 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
Highlighted
Employee
12 Views

Thank you for reporting this. I'll investigate.

0 Kudos
Highlighted
Employee
12 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