- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for reporting this. I'll investigate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ifort handles the polymorphic case CLASS(MyTypeWrapper) successfully but not the case you presented. I reported your case to Development.
(Internal tracking id: DPD200414065)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page