No virtual destructors in fortran 2003?

Do I understand correctly that there are no virtual destructors in F2k3 ?

stefanos-imac:oop borini$ cat a.f90
module AModule
   type :: AType
      contains
      final :: A_dtor
   end type
contains
   subroutine A_dtor(self)
      type(AType), intent(inout) :: self

      print *, "A_dtor"

   end subroutine
end

stefanos-imac:oop borini$ cat b.f90 
module BModule
   use AModule
   type,extends(AType) :: BType
      contains
      final :: B_dtor
   end type
contains
   subroutine B_dtor(self)
      type(BType), intent(inout) :: self

      print *, "B_dtor"

   end subroutine
end

stefanos-imac:oop borini$ cat x.f90 
program x
   use AModule
   use BModule

   class (AType), pointer :: baseptr
   type(BType), pointer :: derivedptr

   allocate(derivedptr)
   baseptr => derivedptr
   deallocate(baseptr)

end program

stefanos-imac:oop borini$ ./a.out 
 A_dtor
forrtl: severe (173): A pointer passed to DEALLOCATE points to an array that cannot be deallocated
Image              PC                Routine            Line        Source             
a.out              0000000108A731F4  Unknown               Unknown  Unknown
a.out              0000000108A7198E  Unknown               Unknown  Unknown
a.out              0000000108A4D791  Unknown               Unknown  Unknown
a.out              0000000108A2283E  Unknown               Unknown  Unknown
a.out              0000000108A3B930  Unknown               Unknown  Unknown
a.out              0000000108A1EF10  Unknown               Unknown  Unknown
a.out              0000000108A0A104  Unknown               Unknown  Unknown
a.out              0000000108A09F0C  Unknown               Unknown  Unknown
a.out              0000000108A09EC4  Unknown               Unknown  Unknown

Answers


I am not so sure about the C++ or Java terminology you use (virtual). I would prefer using Fortran terms when speaking about Fortran. In Fortran terms finalization procedures are not inherited and cannot be overriden. Finalization procedures of both the new type and the extended type should be executed.

As far as I understand the problem, even though I do not have a compiler with a good support for it, your program seems correct. I would expect output:

B_dtor
A_dtor

Do not make assumptions about Fortran 2003 and 2008 standards form some specific compilers, because there are no true Fortran 2003 compilers yet, eventhough couple of them pretend to be. Intel Fortran AFAIK does not claim to be fully Fortran 2003 compliant.


Need Your Help

How can I debug e-mail sending on Gitlab?

ruby-on-rails ruby gitlab

My Gitlab (version 5) is not sending any e-mails and I am lost trying to figure out what is happening. The logs give no useful information. I configured it to used sendmail.

Adjust parameters of serial port reading

c++ windows serial-port

I'm facing a particular issue that regards serial communication under win32.