# Type constructor and final behavior

**URL:** <https://fortran-lang.discourse.group/t/type-constructor-and-final-behavior/2984>\
**Category:** Uncategorized\
**Created:** [March 16, 2022, 12:20pm UTC](https://fortran-lang.discourse.group/t/type-constructor-and-final-behavior/2984 "2022-03-16T12:20:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [March 16, 2022, 1:00pm UTC](https://fortran-lang.discourse.group/t/type-constructor-and-final-behavior/2984/2 "2022-03-16T13:00:47Z")

</div>

> [@hakostra](#):
>
> 1. Is it a gfortran bug that the FINAL routine is never called?

Apparently not. An answer can be found in the thread [Automatic finalization of derived types](https://fortran-lang.discourse.group/t/automatic-finalization-of-derived-types/2295). Quoting the answer there:

> **7.5.6.4 Entities that are not finalized**
> 
> 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.
> 
> _NOTE 1_  
> A nonpointer, nonallocatable object that has the SAVE attribute is never finalized as a direct consequence of the execution of a RETURN or END statement.

> [@hakostra](#):
>
> 1. Can this be written in a way such that there is a consistent behavior (created once and destroyed once) among compilers, without using a manual cleanup?

According to thread I just mentioned, your options are to wrap the contents of the main program in a `block` or a subroutine.

> [@hakostra](#):
>
> 1. Is it correct that Intel calls the FINAL routine twice?

This has to do with intrinsic assignment of polymorphic entities. See [Finalization/Copy in intrinsic polymorphic assignment](https://fortran-lang.discourse.group/t/finalization-copy-in-intrinsic-polymorphic-assignment/2345). I’ve only skimmed that thread, but according to my understanding, the `banana` on the left-hand side is finalized before assignment, after assignment, the `leaky_t` temporary object is also finalized. No finalization is performed at the end due to point 7.5.6.4.

---

_[View the full topic](https://fortran-lang.discourse.group/t/type-constructor-and-final-behavior/2984)._
