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

FINAL subroutine not called

Satish_BD
Beginner
1,858 Views
Hi !
I was experimenting with the "FINAL" subroutines (aka destructors).
In the following piece of code, I create an object and it goes out-of-scope at the end of the program.
I think this is when FINAL subroutine is called. But I don't see the print statement at all.
======= Begin finalize.f90 ==========
[fortran]module m_test_final
implicit none

type :: t_test_final
   integer :: x
contains
   final :: delete
end type t_test_final

contains

   subroutine delete(self)
      type(t_test_final) :: self

      write(*,*) "Destructor called"

   end subroutine delete

end module m_test_final
program main
use m_test_final
implicit none

type(t_test_final) :: test_final

test_final%x = 10

end program main

[/fortran]
======= End finalize.f90 ==========
Compiled with:
ifort -g finalize.f90 && ./a.out
<< no output >>
Am I doing something wrong ?
--Satish
0 Kudos
8 Replies
Satish_BD
Beginner
1,858 Views
The latest one:
Intel Fortran Compiler XE for applications running on IA-32, Version 12.0.3.174 Build 20110309
Copyright (C) 1985-2011 Intel Corporation. All rights reserved.
FOR NON-COMMERCIAL USE ONLY
0 Kudos
mecej4
Honored Contributor III
1,858 Views
Termination of a program ... by execution of the end statement in the main program does not invoke any final subroutines. -- Sec. 16.8, Metcalf, Reid and Cohen, Fortran 95/2003 Explained.
0 Kudos
Satish_BD
Beginner
1,858 Views
Ahan, i see. Thanks. Then how do I safely ensure the clean up (i.e. deallocation, free pointers, etc.) ? I wonder why the Fortran standard says so...
0 Kudos
Ron_Green
Moderator
1,858 Views
$ more finalize.f90
module m_test_final
implicit none

type :: t_test_final
integer :: x
contains
final :: delete
end type t_test_final

contains

subroutine delete(self)
type(t_test_final) :: self

write(*,*) "Destructor called"
end subroutine delete

end module m_test_final
program main
implicit none

call tfinal()

contains
subroutine tfinal()
use m_test_final
type(t_test_final) :: test_final

test_final%x = 10

end subroutine tfinal

end program main

rwgreen@orcsle100:~$ ifort -o finalize finalize.f90
rwgreen@orcsle100:~$ ./finalize
Destructor called
rwgreen@orcsle100:~$

0 Kudos
Satish_BD
Beginner
1,858 Views
Good idea ! That means, I do not directly declare any variables in the "program main" instead wrap it up it another subroutine. Thanks !
0 Kudos
mecej4
Honored Contributor III
1,858 Views
>I wonder why the Fortran standard says so...

They do not publish background discussions in widely available form, but I can see at least one explanation.

Common operating systems routinely clean up when a process finishes execution. A main program calling a FINAL procedure just before the process-terminating END statement may duplicate some chores that the OS will perform, which would be unnecessary and wasteful.

In Linux/Unix a process returns an integer exit code whose interpretation can be documented using the same kind of messages that your FINAL procedure put out. This again, would be an instance of duplicated functionality.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,858 Views
>>Termination of a program ... by execution of the end statement in the main program does not invoke any final subroutines. -- Sec. 16.8, Metcalf, Reid and Cohen, Fortran 95/2003 Explained

Also, this means that unlike C++ the "dtor" is not called on program terminationfor static objects (e.g. in modules and/or SAVE).

Jim Dempsey
0 Kudos
cepheid
Beginner
1,858 Views
Sir,
0 Kudos
Reply