From 81c7ac2b438bdbd90bffe2fc6011c3f6d6c564d4 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Thu, 22 Aug 2024 16:10:26 +0200
Subject: [PATCH] Philippe 22/08/2024: add finalizer/destructor procedure for
 tdimsnc type

---
 src/LIB/SURCOUCHE/src/modd_io.f90           |  2 +-
 src/LIB/SURCOUCHE/src/modd_netcdf.f90       | 17 ++++++++++++++++-
 src/LIB/SURCOUCHE/src/mode_io_tools_nc4.f90 |  4 +---
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/LIB/SURCOUCHE/src/modd_io.f90 b/src/LIB/SURCOUCHE/src/modd_io.f90
index 9adeafca2..30e10a476 100644
--- a/src/LIB/SURCOUCHE/src/modd_io.f90
+++ b/src/LIB/SURCOUCHE/src/modd_io.f90
@@ -136,7 +136,7 @@ TYPE TFILEDATA
   INTEGER(KIND=CDFINT)   :: NNCCOMPRESS_LOSSY_NSD  = 3                  ! Number of Significant Digits (or Bits)
   TYPE(TDIMSNC), POINTER :: TNCDIMS => NULL()     ! Dimensions of netCDF file
   INTEGER(KIND=CDFINT), DIMENSION(:), ALLOCATABLE :: NBOXNCID   ! Box HDF group identifiers     (used for MNHOUTPUT files)
-  TYPE(TDIMSNC),        DIMENSION(:), POINTER :: TBOXNCDIMS => NULL() ! Box dimensions of netCDF file (used for MNHOUTPUT files)
+  TYPE(TDIMSNC),        DIMENSION(:), POINTER     :: TBOXNCDIMS => NULL() ! Box dimensions of netCDF file (used for MNHOUTPUT files)
 #endif
   !
   !Fields for other files
diff --git a/src/LIB/SURCOUCHE/src/modd_netcdf.f90 b/src/LIB/SURCOUCHE/src/modd_netcdf.f90
index f2e506775..814e2c98d 100644
--- a/src/LIB/SURCOUCHE/src/modd_netcdf.f90
+++ b/src/LIB/SURCOUCHE/src/modd_netcdf.f90
@@ -1,4 +1,4 @@
-!MNH_LIC Copyright 1994-2020 CNRS, Meteo-France and Universite Paul Sabatier
+!MNH_LIC Copyright 1994-2024 CNRS, Meteo-France and Universite Paul Sabatier
 !MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 !MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 !MNH_LIC for details. version 1.
@@ -15,6 +15,8 @@ implicit none
 
 public
 
+private :: tdimsnc_destructor
+
 integer, parameter :: NMAXDIMNAMELGTNC4 = 16
 
 !Datatype to store metadata of 1 dimension
@@ -30,6 +32,19 @@ type tdimsnc
   type(tdimnc), dimension(:), pointer :: tdims => null()
   integer :: nmaxdims_str = 0                          ! For character strings
   type(tdimnc), dimension(:), pointer :: tdims_str => null() ! For character strings
+
+  contains
+    private
+    final :: tdimsnc_destructor
 end type tdimsnc
 
+contains
+
+elemental subroutine tdimsnc_destructor( tpdimsnc)
+  type(tdimsnc), intent(inout) :: tpdimsnc
+
+  if ( associated( tpdimsnc%tdims     ) ) deallocate( tpdimsnc%tdims     )
+  if ( associated( tpdimsnc%tdims_str ) ) deallocate( tpdimsnc%tdims_str )
+end subroutine tdimsnc_destructor
+
 end module modd_netcdf
diff --git a/src/LIB/SURCOUCHE/src/mode_io_tools_nc4.f90 b/src/LIB/SURCOUCHE/src/mode_io_tools_nc4.f90
index 90c0f220f..e1a4a866b 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_tools_nc4.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_tools_nc4.f90
@@ -696,9 +696,7 @@ TYPE(tdimsnc),  POINTER :: tpdimsnc
 
 CALL PRINT_MSG(NVERB_DEBUG,'IO','IO_Iocdf_dealloc_nc4','called')
 
-if ( Associated( tpdimsnc%tdims     ) ) deallocate( tpdimsnc%tdims     )
-if ( Associated( tpdimsnc%tdims_str ) ) deallocate( tpdimsnc%tdims_str )
-
+! tpdimsnc components are deallocated / cleaned in the tdimsnc finalizer
 deallocate( tpdimsnc )
 tpdimsnc => Null()
 
-- 
GitLab