# Finalization of array of derived type

**URL:** https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259
**Category:** Help
**Created:** [February 22, 2023, 4:02pm UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259 "2023-02-22T16:02:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![elseifthen](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@elseifthen](https://fortran-lang.discourse.group/u/elseifthen)
#### Post date: [February 22, 2023, 4:02pm UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259/1 "2023-02-22T16:02:35Z")

</div>

I’d like to use finalization to do the clean-up of allocated memory in an array of a derived type but I’m not sure how I’d get the routine referenced in the type’s FINAL statement, DESTROY\_TYPE2, to be called upon deallocation of the derived type. Is there a way to achieve what I’m attempting without looping of type2 and deallocating pd\_1 manually?

```auto
MODULE test
   TYPE t_type1
      TYPE(t_type2), DIMENSION(:), POINTER :: type2 => NULL()
   END TYPE t_type1

   TYPE t_type2
      DOUBLE PRECISION, DIMENSION(:), POINTER :: pd_1 => NULL()
   CONTAINS
      FINAL :: DESTROY_TYPE2

   END TYPE t_type2

CONTAINS
   SUBROUTINE DESTROY_TYPE2(this)
      TYPE(t_type2) :: this
      WRITE(*,*) "Deallocating type2"
      DEALLOCATE(this%pd_1)
   END SUBROUTINE DESTROY_TYPE2

END MODULE test
PROGRAM testprog
  USE test
  INTEGER :: i
  TYPE(t_type1), POINTER :: type1

  ALLOCATE(type1)
  ALLOCATE(type1%type2(3))
  DO i = 1, SIZE(type1%type2)
     ALLOCATE(type1%type2(i)%pd_1(2*i))
     type1%type2(i)%pd_1(:) = i
  END DO
  DO i = 1, SIZE(type1%type2)
     WRITE(*,*) type1%type2(i)%pd_1
  END DO

  DEALLOCATE(type1%type2)
  DEALLOCATE(type1)

END PROGRAM testprog

```

---

<div class="post-metadata">

### Author: ![lineinthesand](https://avatars.discourse-cdn.com/v4/letter/l/a9a28c/32.png) [@lineinthesand](https://fortran-lang.discourse.group/u/lineinthesand)
#### Post date: [February 22, 2023, 4:54pm UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259/2 "2023-02-22T16:54:36Z")

</div>

It does work using a FINAL routine that accepts array of the type with manual looping; not sure if that’s the “correct” way of doing things, though:

```auto
MODULE test
   TYPE t_type1
      TYPE(t_type2), DIMENSION(:), POINTER :: type2 => NULL()
   END TYPE t_type1

   TYPE t_type2
      DOUBLE PRECISION, DIMENSION(:), POINTER :: pd_1 => NULL()
   CONTAINS
      FINAL :: DESTROY_TYPE2_ARR

   END TYPE t_type2

CONTAINS
   SUBROUTINE DESTROY_TYPE2_ARR(this)
      TYPE(t_type2), DIMENSION(:) :: this
      INTEGER :: i
      WRITE(*,*) "Deallocating type2 arr"
      DO i = 1, SIZE(this)
         CALL DESTROY_TYPE2(this(i))
      END DO
   END SUBROUTINE DESTROY_TYPE2_ARR

   SUBROUTINE DESTROY_TYPE2(this)
      TYPE(t_type2) :: this
      WRITE(*,*) "Deallocating type2"
      DEALLOCATE(this%pd_1)
   END SUBROUTINE DESTROY_TYPE2

END MODULE test
PROGRAM testprog
  USE test
  INTEGER :: i
  TYPE(t_type1), POINTER :: type1

  ALLOCATE(type1)
  ALLOCATE(type1%type2(3))
  DO i = 1, SIZE(type1%type2)
     ALLOCATE(type1%type2(i)%pd_1(2*i))
     type1%type2(i)%pd_1(:) = i
  END DO
  DO i = 1, SIZE(type1%type2)
     WRITE(*,*) type1%type2(i)%pd_1
  END DO

  DEALLOCATE(type1%type2)
  DEALLOCATE(type1)

END PROGRAM testprog

```

---

<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: [February 22, 2023, 6:29pm UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259/3 "2023-02-22T18:29:58Z")

</div>

> [@elseifthen](#):
>
> … Is there a way to achieve what I’m attempting without looping of type2 and deallocating pd\_1 manually? …

@elseifthen , @lineinthesand ,

Note there isnt necessarily a “correct” way of doing this, you have to think like an architectural engineer and see what works best based on different considerations: functionality, efficiency, aesthetics, etc.

An immediate option is to make the finalizer `elemental`.

---

<div class="post-metadata">

### Author: ![elseifthen](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@elseifthen](https://fortran-lang.discourse.group/u/elseifthen)
#### Post date: [February 23, 2023, 7:45am UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259/4 "2023-02-23T07:45:54Z")

</div>

Thanks for the hint with `elemental`. Unfortunately, I cannot use it in my actual case as the deallocation routines _do_ have side effects as they modify global variables.  
So I’ll use the “array of type” finalization.

---

<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: [February 23, 2023, 9:54am UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259/5 "2023-02-23T09:54:41Z")

</div>

Do we agree that if the components were `allocatable` instead of `pointer`, there would be no need for custom finalization?

---

<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: [February 23, 2023, 11:42am UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259/6 "2023-02-23T11:42:41Z")

</div>

> [@elseifthen](#):
>
> Thanks for the hint with `elemental`. Unfortunately, I cannot use it in my actual case as the deallocation routines _do_ have side effects as they modify global variables.  
> So I’ll use the “array of type” finalization.

@elseifthen ,

Sure an “array type” finalizer can be used, note in.a general scenario an author of a finalizable type requiring a finalizer may then need to implement finalizers of rank-0, rank-1, rank-2, etc. depending on how the customers may consume said type and which can be a guessing game.

Or, there is also the option of `IMPURE ELEMENTAL` for a finalizer with “side effects”!

---

<div class="post-metadata">

### Author: ![gardhor](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gardhor/32/88_2.png) [@gardhor](https://fortran-lang.discourse.group/u/gardhor)
#### Post date: [February 24, 2023, 6:37am UTC](https://fortran-lang.discourse.group/t/finalization-of-array-of-derived-type/5259/7 "2023-02-24T06:37:27Z")

</div>

I think, it is better to add a nullify statement after DEALLOCATE(this%pd\_1) in the DESTROY\_TYPE2 subroutine.  
So that, the status of the pointer is well defined to null().

> [@lineinthesand](#):
>
> ```auto
> SUBROUTINE DESTROY_TYPE2(this)
> TYPE(t_type2) :: this
> WRITE(*,*) "Deallocating type2"
> DEALLOCATE(this%pd_1)
> NULLIFY(this%pd_1)
> END SUBROUTINE DESTROY_TYPE2
> 
> ```
