FINAL subroutine not called

FINAL subroutine not called

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 ==========

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

======= End finalize.f90 ==========Compiled with:ifort -g finalize.f90 && ./a.out<< no output >>Am I doing something wrong ?--Satish

publicaciones de 9 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.

The latest one:Intel Fortran Compiler XE for applications running on IA-32, Version 12.0.3.174 Build 20110309Copyright (C) 1985-2011 Intel Corporation. All rights reserved.FOR NON-COMMERCIAL USE ONLY

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.

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...

$ 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:~$

Good idea ! That means, I do not directly declare any variables in the "program main" instead wrap it up it another subroutine. Thanks !

>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.

>>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

www.quickthreadprogramming.com

Sir,

Deje un comentario

Por favor inicie sesión para agregar un comentario. ¿No es socio? Únase ya