- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
$ 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:~$
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:~$
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Good idea ! That means, I do not directly declare any variables in the "program main" instead wrap it up it another subroutine. Thanks !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sir,

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page