# Should associate trigger automatic finalization?

**URL:** <https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324>\
**Category:** Help\
**Created:** [November 23, 2021, 1:33pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324 "2021-11-23T13:33:18Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [November 23, 2021, 1:33pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/1 "2021-11-23T13:33:18Z")

</div>

Consider the following program:

```fortran
module associate_example

  implicit none

  type :: sparse
    integer :: colidx
    integer, allocatable :: rows(:)
    real, allocatable :: values(:)
  contains
    final :: finalize
  end type

contains

  subroutine finalize(this)
    type(sparse) :: this

    print *, "Finalizing column ", this%colidx
  end subroutine

end module associate_example

program associate_test

  use associate_example
  implicit none

  associate(s => sparse(3,[1,2,3],[5.,3.,8.]))

    print *, "In associate:"
    print *, s%colidx
    print *, s%rows
    print *, s%values

  end associate

  block
    type(sparse) :: s

    print *, "In block:"

    s = sparse(3,[1,2,3],[5.,3.,8.])

    print *, s%colidx
    print *, s%rows
    print *, s%values

  end block

end program

```

Both gfortran (v10.3) and ifort (v2021.4) compile the program without warnings. The output of gfortran is:

```nohighlight
$ gfortran -Wall associate_test.f90 
$ ./a.out
munmap_chunk(): invalid pointer

Program received signal SIGABRT: Process abort signal.

```

The output of Intel Fortran is:

```nohighlight
$ ifort -warn all associate_test.f90 
$ ./a.out
 In associate:
           3
           1 2 3
   5.000000 3.000000 8.000000    
 In block:
 Finalizing column 0
           3
           1 2 3
   5.000000 3.000000 8.000000    
 Finalizing column 3

```

I would expect finalization to occur at the end of the associate construct.

Is this mandated by the standard, or should we avoid associating with an expression that results in a derived type?

In the result from ifort, does the output line finalizing column 0 come from the finalization of the left-hand side in the assignment?

---

<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:** [November 23, 2021, 1:58pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/2 "2021-11-23T13:58:12Z")

</div>

If I overload the structure constructor by adding a custom constructor function:

```fortran
  interface sparse
    module procedure new_sparse
  end interface

```

```fortran
  function new_sparse(idx,rows,values) result(new)
    integer, intent(in) :: idx
    integer, intent(in) :: rows(:)
    real, intent(in) :: values(:)
    type(sparse) :: new

    new%colidx = idx
    allocate(new%rows,source=rows)
    allocate(new%values,source=values)

  end function

```

I get the behavior I expected:

```nohighlight
$ ifort -warn all associate_test.f90 
(base) ipribec@ipribec-T530:~/fortran$ ./a.out
 In associate:
           3
           1 2 3
   5.000000 3.000000 8.000000    
 Finalizing column 3
 In block:
 Finalizing column 0
 Finalizing column 3
           3
           1 2 3
   5.000000 3.000000 8.000000    
 Finalizing column 3

```

---

<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:** [November 23, 2021, 2:50pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/3 "2021-11-23T14:50:41Z")

</div>

In the case of gfortran, I have submitted several bug reports related to memory errors in the use of `associate`.

In the case of Intel, without the overloaded constructor, it looks as though some “optimizations” are being made, that may or may not be standards conforming. Perhaps someone with a more thorough understanding of this part of the standard could comment on whether this is allowed.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [November 23, 2021, 3:34pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/4 "2021-11-23T15:34:49Z")

</div>

> [@ivanpribec](#):
>
> … I would expect finalization to occur at the end of the associate construct.
> 
> Is this mandated by the standard, or should we avoid associating with an expression that results in a derived type? …

Per my understanding, the standard is silent on the question of the `ASSOCIATE` construct when the selector is an expression of a finalizable entity. Thus the chances are zero any of the compiler implementations will perform finalization in the first case shown here.

If finalization is important, then as a matter of good practice it does make sense to “avoid associating with an expression that results in a derived type.” But instead to make the selector an object that will be finalized as per the semantics stated in the standard.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [November 23, 2021, 3:36pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/5 "2021-11-23T15:36:54Z")

</div>

> [@ivanpribec](#):
>
> … In the result from ifort, does the output line finalizing column 0 come from the finalization of the left-hand side in the assignment?

Re: “the output line finalizing column 0,” it is the LHS in the assignment `s = sparse(3, ..)` in the `BLOCK` construct getting finalized as per the standard.

---

<div class="post-metadata">

**Author:** ![Reinhold\_Bader](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Reinhold\_Bader](https://fortran-lang.discourse.group/u/Reinhold_Bader)\
**Post date:** [November 23, 2021, 3:39pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/6 "2021-11-23T15:39:37Z")

</div>

For the case where the structure constructor is used: no finalization should happen here at all, because use of the structure constructor is not one of the trigger cases.

For the case where a function overloads the structure constructor, the following text from the standard applies, I think:

F2018 / 7.5.6.3 when finalization occurs  
…  
If an executable construct references a nonpointer function, the result is finalized after execution of the innermost executable construct containing the reference.  
…  
which I take to mean that the finalizer must be executed once the ASSOCIATE block completes.

Regards  
Reinhold

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [November 23, 2021, 3:53pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/7 "2021-11-23T15:53:15Z")

</div>

> [@Reinhold\_Bader](#):
>
> For the case where a function overloads the structure constructor, the following text from the standard applies, I think:
> 
> F2018 / 7.5.6.3 when finalization occurs  
> …  
> If an executable construct references a nonpointer function, the result is finalized after execution of the innermost executable construct containing the reference.  
> …  
> which I take to mean that the finalizer must be executed once the ASSOCIATE block completes.

@Reinhold_Bader , welcome to the forum.

My hunch is compiler implementations will find the `associate-stmt` as not an executable construct and not do the finalization, that the instructions in the block part of the construct, the stuff between the `associate-stmt` and the `end-associate-stmt` is what they consider an executable-construct here.

---

<div class="post-metadata">

**Author:** ![Reinhold\_Bader](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Reinhold\_Bader](https://fortran-lang.discourse.group/u/Reinhold_Bader)\
**Post date:** [November 23, 2021, 4:10pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/8 "2021-11-23T16:10:43Z")

</div>

Maybe some people do, but the standard text does consider the associate-stmt as part of the associate-construct (F2018 / 11.1.3.1).

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 23, 2021, 4:19pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/9 "2021-11-23T16:19:21Z")

</div>

@Reinhold_Bader thanks for joining the forum and your feedback!

---

<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:** [November 23, 2021, 4:28pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/10 "2021-11-23T16:28:13Z")

</div>

Thanks Reinhold for the answer.

The example I constructed here is artificial in the sense that the derived type does not need a custom finalizer. However for any object which requires custom finalization (e.g. because it contains pointers) it’s probably a good idea to overload the structure constructor.

This seems very close to the programming idiom/pattern known as the “Rule of zero/three/five” in C++:

- [The rule of three/five/zero (cppreference.com)](https://en.cppreference.com/w/cpp/language/rule_of_three)
- [Rule of three (Wikipedia)](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming))

---

<div class="post-metadata">

**Author:** ![Reinhold\_Bader](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Reinhold\_Bader](https://fortran-lang.discourse.group/u/Reinhold_Bader)\
**Post date:** [November 23, 2021, 4:36pm UTC](https://fortran-lang.discourse.group/t/should-associate-trigger-automatic-finalization/2324/11 "2021-11-23T16:36:27Z")

</div>

I agree. The structure constructor is so restricted in semantics that executing a finalizer is simply not necessary. But for the same reason, it usually cannot do what needs to be done for POINTER components.

Cheers.
