Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks and other issues identified by the code sanitizer #1353

Merged
merged 7 commits into from
Nov 10, 2022
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This file documents all notable changes to the GEOS-Chem repository since versio

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased [14.0.2]
### Changed
- Rewrote code to avoid memory leaks (identified by the code sanitizer)
yantosca marked this conversation as resolved.
Show resolved Hide resolved

## [14.0.1] - 2022-10-31
### Fixed
- Corrected units in metadata for State_Met%AirNumDen and State_Met%PHIS
Expand Down
35 changes: 35 additions & 0 deletions Headers/input_opt_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,13 @@ SUBROUTINE Cleanup_Input_Opt( Input_Opt, RC )
Input_Opt%FAM_NAME => NULL()
ENDIF

IF ( ASSOCIATED( Input_Opt%FAM_TYPE ) ) THEN
DEALLOCATE( Input_Opt%FAM_TYPE, STAT=RC )
CALL GC_CheckVar( 'Input_Opt%FAM_TYPE', 2, RC )
IF ( RC /= GC_SUCCESS ) RETURN
Input_Opt%FAM_TYPE => NULL()
ENDIF

IF ( ASSOCIATED( Input_Opt%LINOZ_TPARM ) ) THEN
DEALLOCATE( Input_Opt%LINOZ_TPARM, STAT=RC )
CALL GC_CheckVar( 'Input_Opt%LINOZ_TPARM', 2, RC )
Expand All @@ -1226,6 +1233,34 @@ SUBROUTINE Cleanup_Input_Opt( Input_Opt, RC )
Input_Opt%LSPECRADMENU => NULL()
ENDIF

IF ( ASSOCIATED( Input_Opt%LSKYRAD ) ) THEN
DEALLOCATE( Input_Opt%LSKYRAD, STAT=RC )
CALL GC_CheckVar( 'Input_Opt%LSKYRAD', 2, RC )
IF ( RC /= GC_SUCCESS ) RETURN
Input_Opt%LSKYRAD => NULL()
ENDIF

IF ( ASSOCIATED( Input_Opt%WVSELECT ) ) THEN
DEALLOCATE( Input_Opt%WVSELECT, STAT=RC )
CALL GC_CheckVar( 'Input_Opt%WVSELECT', 2, RC )
IF ( RC /= GC_SUCCESS ) RETURN
Input_Opt%WVSELECT => NULL()
ENDIF

IF ( ASSOCIATED( Input_Opt%STRWVSELECT ) ) THEN
DEALLOCATE( Input_Opt%STRWVSELECT, STAT=RC )
CALL GC_CheckVar( 'Input_Opt%STRWVSELECT', 2, RC )
IF ( RC /= GC_SUCCESS ) RETURN
Input_Opt%STRWVSELECT => NULL()
ENDIF

IF ( ASSOCIATED( Input_Opt%ObsPack_SpcName ) ) THEN
DEALLOCATE( Input_Opt%ObsPack_SpcName, STAT=RC )
CALL GC_CheckVar( 'Input_Opt%ObsPack_SpcName', 2, RC )
IF ( RC /= GC_SUCCESS ) RETURN
Input_Opt%ObsPack_SpcName => NULL()
ENDIF

#ifdef MODEL_GEOS
!=======================================================================
! These fields of Input_Opt are only finalized when
Expand Down
4 changes: 4 additions & 0 deletions Headers/registry_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ SUBROUTINE Registry_AddField( Input_Opt, Registry, State, &
RETURN
ENDIF

! Free Item once we have attached it to the Registry linked list
DEALLOCATE( Item )
Item => NULL()

END SUBROUTINE Registry_AddField
!EOC
!------------------------------------------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions History/history_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ SUBROUTINE History_ReadCollectionData( Input_Opt, State_Chm, State_Diag, &
! Increment the item count
ItemCount = ItemCount + 1

! Create the a HISTORY ITEM object for this diagnostic
! Create the a HISTORY ITEM object for this diagnostic
! and add it to the given DIAGNOSTIC COLLECTION
CALL History_AddItemToCollection( &
Input_Opt = Input_Opt, &
Expand Down Expand Up @@ -1770,7 +1770,6 @@ SUBROUTINE History_ReadCollectionData( Input_Opt, State_Chm, State_Diag, &
RETURN
ENDIF
ENDIF

ENDDO

!=======================================================================
Expand Down Expand Up @@ -2345,6 +2344,10 @@ SUBROUTINE History_AddItemToCollection( Input_Opt, &
! Increment number of HISTORY ITEMS in this collection
Collection%nHistItems = Collection%nHistItems + 1

! Free Item (we have added it to the container and don't need it anymore)
DEALLOCATE( Item )
Item => NULL()

! Free pointers
Ptr0d => NULL()
Ptr0d_8 => NULL()
Expand Down
6 changes: 5 additions & 1 deletion History/history_netcdf_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ SUBROUTINE History_Netcdf_Close( Container, RC )
!
! !LOCAL VARIABLES:
!
TYPE(MetaHIstItem), POINTER :: Current
TYPE(MetaHistItem), POINTER :: Current

!=======================================================================
! Initialize
Expand Down Expand Up @@ -1677,6 +1677,10 @@ SUBROUTINE IndexVarList_Create( Input_Opt, Container, IndexVarList, RC )
CALL GC_Error( ErrMsg, RC, ThisLoc )
RETURN
ENDIF

! Free Item now that we have added it to IndexVarList
DEALLOCATE( Item )
Item => NULL()
ENDDO

END SUBROUTINE IndexVarList_Create
Expand Down
1 change: 1 addition & 0 deletions Interfaces/GCClassic/main.F90
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ PROGRAM GEOS_Chem

! Run HEMCO phase 0 as simplfied phase 1 to get initial met fields
! and restart file fields
TimeForEmis = .FALSE.
CALL Emissions_Run( Input_Opt, State_Chm, State_Diag, State_Grid, &
State_Met, TimeForEmis, 0, RC )

Expand Down