module testmode implicit none type :: simple integer :: ind = -1 contains final :: destructor1 end type simple integer :: final_count = 0 contains subroutine destructor1(self) type(simple), intent(inout) :: self final_count = final_count + 1 print *, "# Leave desctructor1:", final_count, self% ind self% ind = - self% ind end subroutine destructor1 end module testmode program test_final use, intrinsic :: iso_fortran_env use testmode implicit none type(simple), parameter :: ThyType_param = simple(20) character(80) :: compiler compiler = compiler_version () print * print *, "At start of program :", final_count call reset () print * call sub () print * print *, "After sub :", final_count contains subroutine sub () type(simple), parameter :: ThyType = simple(21) type(simple) :: ThyType2 = simple(22) type(simple), allocatable :: MyType, MyType2 print *, "Enter sub :", final_count call reset () MyType = ThyType print *, "After 1st allocation:", final_count MyType2 = ThyType2 print *, "After 2nd allocation:", final_count print *, "Checking MyType% ind:", MyType% ind print *, "Checking MyType2%ind:", MyType2% ind if (.not. allocated (MyType )) print *, "MyType?" if (.not. allocated (MyType2)) print *, "MyType2?" print *, "Deallocate MyType :", final_count deallocate (MyType) print *, "* MyType deallocated:", final_count if (allocated (MyType2)) & print *, "(kept MyType2 for automatic deallocation on return from sub)" print *, "Leave sub :", final_count end subroutine sub ! subroutine reset () if (final_count == 0) return if (compiler(1:4) == "Cray") then print *, "+++ Resetting final_count for ", trim (compiler) final_count = 0 ! reset for crayftn 12.0.2 end if end subroutine reset end program test_final