I have an object parsing a textfile. Here\'s my main program:
program main
use Parser_class
implicit none
type(Parser) :: Parser
call Parser%ProcessFile(\'data.
In the Fortran 2008 standard, when finalization comes about is given in section 4.5.6.31. I won't copy all the times here, but I will summarize.
What is clearly mentioned following from when, is when not:
If image execution is terminated, either by an error (e.g. an allocation failure) or by execution of a stop-stmt, error-stop-stmt, or end-program-stmt, entities existing immediately prior to termination are not finalized.
This covers your program. Parser
is in the program's scope and it still exists at the end of the program. There are no apparent other things which would cause finalization.
If Deallocate
is a final procedure for the type, then there are subtle ways in which finalization of an object of that type differ from a call of the type-bound procedure. In finalization, the process is recursive: components and parents are themselves subject to finalization. With a subroutine call that recursion must appear manually in some way.
In many cases one doesn't care that an entity isn't finalized at the end of the program. After all, any deallocation is then the operating system's problem, not the programmer's. However, there are times other forms of tidying up are indeed desirable.
True finalization can be forced in some ways. If the list below is examined, two options come to mind:
Parser
object allocatable and explicitly deallocate it;block
construct.To crudely summarize when finalization happens:
intent(out)
arguments;1If you aren't reading the final form of the document you'll want to pretend paragraphs 5 and 7 don't exist.