From 94aa27a7e826719e31f95846af19a41a1e201f88 Mon Sep 17 00:00:00 2001 From: likeuclinux Date: Tue, 4 Feb 2025 16:59:46 -0500 Subject: [PATCH] Fix memory leaks identified by a PGI compiler (#2139) TYPE: bug fix KEYWORDS: Memory Leaks SOURCE: Charlie Li - Software developer from lakes environmental Canada DESCRIPTION OF CHANGES: Problem: Memory leaks are detected in wrf_timeseries.F and start_em.F when use PGI option: -g -O0 -traceback -Mchkptr -Mbounds -Ktrap=fp -Msave -tp=px It will failed for: "0: ALLOCATE: array already allocated" 1. In dyn_em/start_em.F, dz8w is allocated, but not deallocated. 2. In share/wrf_timeseries.F, two arrays, earth_u_profile and earth_v_profile, are allocated without being deallocated when time series is not computed. Solution: Calls to deallocate the array dz8w is added in start_em.F, and move the return statement before array allocation in wrf_timeseries.F. LIST OF MODIFIED FILES: M share/wrf_timeseries.F M dyn_em/start_em.F TESTS CONDUCTED: The Jenkins tests are all passing. RELEASE NOTE: This PR fixed memory leaks related to arrays being allocated without being deallocated in start_em and time series calculation subroutines. --- dyn_em/start_em.F | 1 + share/wrf_timeseries.F | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/dyn_em/start_em.F b/dyn_em/start_em.F index 97a5bfcdcf..96057ebdfb 100644 --- a/dyn_em/start_em.F +++ b/dyn_em/start_em.F @@ -2001,6 +2001,7 @@ SUBROUTINE start_domain_em ( grid, allowed_to_read & #endif DEALLOCATE(z_at_q) +DEALLOCATE(dz8w) IF (config_flags%p_lev_diags == PRESS_DIAGS ) THEN CALL wrf_debug ( 200 , ' PLD: pressure level diags' ) diff --git a/share/wrf_timeseries.F b/share/wrf_timeseries.F index cab5a0aa10..53ccb27d1f 100644 --- a/share/wrf_timeseries.F +++ b/share/wrf_timeseries.F @@ -351,17 +351,21 @@ SUBROUTINE calc_ts( grid ) ! FALSE to output T and Q at 2-m and wind at 10-m diagnostic levels: LOGICAL, PARAMETER :: ts_model_level = .FALSE. + IF ( grid%ntsloc_domain .LE. 0 ) THEN + RETURN + END IF + +#if ((EM_CORE == 1) && (DA_CORE != 1)) + IF ( grid%dfi_opt /= DFI_NODFI .AND. grid%dfi_stage /= DFI_FST ) THEN + RETURN + END IF +#endif + !Allocate the arrays for wind components #if ( EM_CORE == 1 ) ALLOCATE ( earth_u_profile(grid%max_ts_level), earth_v_profile(grid%max_ts_level) ) #endif - IF ( grid%ntsloc_domain .LE. 0 ) RETURN - -#if ((EM_CORE == 1) && (DA_CORE != 1)) - IF ( grid%dfi_opt /= DFI_NODFI .AND. grid%dfi_stage /= DFI_FST ) RETURN -#endif - n = grid%next_ts_time ALLOCATE(p8w(grid%sm32:grid%em32))