Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:46:40+00:00 2026-06-06T10:46:40+00:00

I have a derived type t_file with a finalization routine close which simply writes

  • 0

I have a derived type t_file with a finalization routine close which simply writes “Finalization” to the screen.
There is also a function returning an instance of the type t_file.
The output of this program is

Finalization.
Finalization.
Just opened
     2000
Done.

I have two questions:

  • Why does the finalization occur before the Just opened output?
  • Why does the finalization occur twice?

My compiler is Intel(R) Visual Fortran Composer XE 2011 12.1.3526.2010.

Here is the code:

module m_file
    implicit none


    type t_file
        integer::iu=1000

        contains

        final::close
    end type

    contains

    function openFile() result(f)
        implicit none

        type(t_file)::f

        f%iu = 2000

    end function

    subroutine close(this)
        implicit none

        type(t_file)::this

        write(*,*) 'Finalization.'

    end subroutine

end module

program foo
    use m_file
    implicit none

    type(t_file)::f

    f = openFile()
    write(*,*) 'Just opened'
    write(*,*) f%iu

    write(*,*) 'Done.'    
    read(*,*)

end program
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:46:42+00:00Added an answer on June 6, 2026 at 10:46 am

    This behaviour surprised me too. I’ve been getting to grips with Fortran’s new(-ish) OO features but haven’t yet needed to write final procedures. I think that I can provide an explanation, of sorts, for this behaviour.

    On p282 of Modern Fortran Explained the authors write:

    When a finalizable object is about to cease to exist (for example, by
    being deallocated or from execution of a return statement), the final
    subroutine is invoked with the object as its actual argument. This
    also occurs when the object is passed to an intent out dummy
    argument, or is the variable on the left-hand side of an intrinsic
    assignment statement. In the latter case, the final subroutine is
    invoked after the expression on the right-hand side has been
    evaluated, but before it is assigned to the variable.

    It looks to me as if you are hitting both of the two cases mentioned in this paragraph. You get the first Finalization when the entity named f inside the function openFile is about to go out of scope on return from that function.

    You get the second Finalization when the variable f in the program scope is used on the lhs of the assignment f = openFile().

    From all of this I conclude that you are not seeing premature finalisation of f in the program scope, but something subtly different.

    I’m not entirely convinced that this is what is happening, and I cannot think of a good reason why the language’s behaviour should be as it is. I’m a bit surprised, now that I’ve looked into it, that you don’t get a third Finalization message as the program ends and f goes out of scope.

    With any luck a real Fortran guru will come past soon and enlighten us all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Show table, and I would like to have a derived type
Let's say I have type A, and a derived type B. When I perform
I have an abstract type A , and two derived types A1 and A2
I have a folder content type that derives from ATBTreeFolder which in plone 4(actually
I have written a method which writes an object to file. I used generics
I have a Fortran derived type T that contains data arrays of (many) different
I have a struct simple_instr that I can't modify. I create a derived type
I have a trait, which takes a type parameter, and I want to say
I have a base class StandardMeasurement and a derived class CustomMeasurement both of which
I have this problem: The Vehicle type derives from the EntityObject type which has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.