# Confused by finalization behaviour

**URL:** <https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720>\
**Category:** Help\
**Created:** [October 15, 2024, 4:57am UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720 "2024-10-15T04:57:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blaise](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@Blaise](https://fortran-lang.discourse.group/u/Blaise)\
**Post date:** [October 15, 2024, 4:57am UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/1 "2024-10-15T04:57:57Z")

</div>

Hi,  
I am having a hard time understanding the behaviour of finalization. See the attached small code:  
[LinAlg1.F90](https://fortran-lang.discourse.group/uploads/short-url/2xETkEXKC4iSNr0OuQMd0gwF5U6.F90) (2.2 KB)  
Here is the output when running on an arm Mac with gfortran 14.2 ( I get a similar output under linux with the most recent intel compilers).

```auto
sibookpro:LinAlg_Class $ ./LinAlg1
Destroying empty
   No need to deallocate empty%data
Destroying u
   Deallocating u%data
Viewing u
   1.00000000 2.00000000 3.00000000 ... 3.00000000    
Destroying u
   Deallocating u%data
Destroying empty
   No need to deallocate empty%data
Destroying v
   Deallocating v%data
Viewing v
   2.33999991 2.33999991 2.33999991 ... 2.33999991    

```

Why is finalization called _twice_ during creation?

---

<div class="post-metadata">

**Author:** ![davidpfister](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/davidpfister/32/3949_2.png) [@davidpfister](https://fortran-lang.discourse.group/u/davidpfister)\
**Post date:** [October 15, 2024, 5:19am UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/2 "2024-10-15T05:19:32Z")

</div>

Hi @Blaise,  
When you use a function constructor, you return an object that serves as a rhs in your assignment

```auto
u = LinAlg_Type('u',x)

```

That object is very short lived and will be finalized once the lhs is allocated/assigned.

If you do not want to get this behavior you need to use subroutine constructors with intent out arguments.

You can have a look at this [discussion](https://fortran-lang.discourse.group/t/finalization-ifort-vs-gfortran/7816/11). You will probably find some more details about ‘final’

---

<div class="post-metadata">

**Author:** ![grofz](https://avatars.discourse-cdn.com/v4/letter/g/8797f3/32.png) [@grofz](https://fortran-lang.discourse.group/u/grofz)\
**Post date:** [October 15, 2024, 5:27am UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/3 "2024-10-15T05:27:16Z")

</div>

When the following statement is called  
u = LinAlg\_Type(‘u’,x)  
actually two things occur:

1. the code inside the function is executed, operating with the internal variable that the function “returns”
2. assignment from this internal returning variable to the variable “u” declared within main program takes place.

So the “u” is finalized first (before the assignment), the second call is for finalizing the internal variable inside the function.

It helps to realize that when inside the function, the function is not working with the variable “u” because the function “does not know” what happens with the result (It could be used, for example, as an intermmediate in an expression).

---

<div class="post-metadata">

**Author:** ![jwmwalrus](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jwmwalrus/32/4483_2.png) [@jwmwalrus](https://fortran-lang.discourse.group/u/jwmwalrus)\
**Post date:** [October 15, 2024, 9:13am UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/4 "2024-10-15T09:13:11Z")

</div>

> [@Blaise](#):
>
> I am having a hard time understanding the behaviour of finalization.

Adding to what others said:

Notice that, since you’re not using pointers, you don’t really need to worry about providing a `final` procedure.

The `allocatable` attribute implies automatic deallocation of any item when it goes out of scope.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [October 15, 2024, 11:27am UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/5 "2024-10-15T11:27:58Z")

</div>

> [@grofz](#):
>
> u = LinAlg\_Type(‘u’,x)  
> actually two things occur:
> 
> 1. the code inside the function is executed, operating with the internal variable that the function “returns”
> 2. assignment from this internal returning variable to the variable “u” declared within main program takes place.

That’s correct, but note that the compiler is free to optimize this statement, for instance by not creating a temporary object but rather operating directly on `u`. In this case a single finalization would occur.

---

<div class="post-metadata">

**Author:** ![Blaise](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@Blaise](https://fortran-lang.discourse.group/u/Blaise)\
**Post date:** [October 15, 2024, 1:34pm UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/6 "2024-10-15T13:34:32Z")

</div>

Thanks for all the comments. It looks like gfortran does not allow using subroutines for the constructor while ifx throws an unrelated error. See attached.  
[LinAlg2.F90](https://fortran-lang.discourse.group/uploads/short-url/4B6sOQBiJgummSkSgSCfaYQQuhi.F90) (2.4 KB)

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 15, 2024, 1:45pm UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/7 "2024-10-15T13:45:43Z")

</div>

> [@PierU](#):
>
> In this case a single finalization would occur.

No, the standard says that both finalizations occur. In the case there is a final subroutine that needs to be called then this is not an optimization the compiler is free to make.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [October 15, 2024, 2:58pm UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/8 "2024-10-15T14:58:32Z")

</div>

> [@everythingfunctional](#):
>
> No, the standard says that both finalizations occur. In the case there is a final subroutine that needs to be called then this is not an optimization the compiler is free to make.

My bad… I remember there is an assignment case where a finalization can be avoided by a smart compiler, but can’t remember which one.

---

<div class="post-metadata">

**Author:** ![davidpfister](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/davidpfister/32/3949_2.png) [@davidpfister](https://fortran-lang.discourse.group/u/davidpfister)\
**Post date:** [October 15, 2024, 3:19pm UTC](https://fortran-lang.discourse.group/t/confused-by-finalization-behaviour/8720/9 "2024-10-15T15:19:42Z")

</div>

Indeed, gfortran throws:

```auto
In generic interface 'linalg_type' at (1) procedures must be all FUNCTIONs as the generic name is also the name of a derived type

```

In this case, you can simply name your interface LinAlg:

```auto
interface LinAlg
   module procedure LinAlg_constructorArray,LinAlg_constructorLength
end interface LinAlg

```

Just check the call in your main program

```auto
u = LinAlg_Type('u',x)

```

should become

```auto
call LinAlg('u',x, u)

```

And what about using an initialization bound procedure:

```auto
type, public :: LinAlg_Type
    character(len=256) :: name = 'empty'
    real,dimension(:),allocatable :: data
 contains
	  procedure, pass(self), private :: LinAlg_constructorArray
	  procedure, pass(self), private :: LinAlg_constructorLength
	  generic, public :: init => LinAlg_constructorArray,LinAlg_constructorLength
    procedure :: view => LinAlg_view
    final :: destroy
end type

```

that you call like this:

```auto
call v%init('v',3,2.34)

```
