Finalization

Contents
Final subroutines
When final runs
Finalize allocatables
Design for cleanup

Final subroutines

A final subroutine is a cleanup routine that the compiler calls on its own when a value of the type is destroyed.

Register it with the final keyword inside the contains block of the type, and give it one argument of that type.

The buffer type below registers a cleanup routine next to its data.

module buffers implicit none type :: buffer integer, allocatable :: data(:) contains final :: cleanup end type buffer contains subroutine cleanup(self) type(buffer), intent(inout) :: self print *, "cleanup runs" end subroutine cleanup end module buffers

When final runs

Finalization runs when a local variable goes out of scope, when an allocatable object is deallocated, and for temporaries the compiler creates.

It does not run for pointers being nullified, since the target may still be alive through other pointers.

The program below shows the simple case: the message appears as the block ends and the buffer is destroyed.

program demo implicit none use buffers block type(buffer) :: b allocate(b%data(4)) print *, "inside block" end block print *, "after block" end program demo

Finalize allocatables

Allocatable components are released on their own, so a final routine is only needed for extra work such as closing files or logging.

Inside cleanup, test the component with the allocated intrinsic before touching it, because the value may arrive empty.

This version prints the buffer size when data exists and stays quiet otherwise.

subroutine cleanup(self) implicit none type(buffer), intent(inout) :: self if (allocated(self%data)) print *, size(self%data) end subroutine cleanup

Design for cleanup

Keep final routines short, safe, and repeatable: they must handle half built values and never stop the program with an error.

Release exactly the resources the type owns, and leave shared objects alone so other owners can continue to use them.

With this discipline every exit path cleans up the same way, and callers never need manual teardown calls.

program demo implicit none use buffers type(buffer), allocatable :: b allocate(b) allocate(b%data(8)) deallocate(b) print *, "done" end program demo

Next: Allocatable arrays

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: