subroutine ESMF_LocStreamDestroy(locstream, keywordEnforcer, noGarbage, rc)
!
! !ARGUMENTS:
type(ESMF_LocStream), intent(inout) :: locstream
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
logical, intent(in), optional :: noGarbage
integer, intent(out), optional :: rc
!
! !STATUS:
! \begin{itemize}
! \item\apiStatusCompatibleVersion{5.2.0r}
! \item\apiStatusModifiedSinceVersion{5.2.0r}
! \begin{description}
! \item[8.1.0] Added argument {\tt noGarbage}.
! The argument provides a mechanism to override the default garbage collection
! mechanism when destroying an ESMF object.
! \end{description}
! \end{itemize}
!
! !DESCRIPTION:
! Deallocate an {\tt ESMF\_LocStream} object and appropriate
! internal structures.
!
! The arguments are:
! \begin{description}
! \item[locstream]
! locstream to destroy
! \item[{[noGarbage]}]
! If set to {\tt .TRUE.} the object will be fully destroyed and removed
! from the ESMF garbage collection system. Note however that under this
! condition ESMF cannot protect against accessing the destroyed object
! through dangling aliases -- a situation which may lead to hard to debug
! application crashes.
!
! It is generally recommended to leave the {\tt noGarbage} argument
! set to {\tt .FALSE.} (the default), and to take advantage of the ESMF
! garbage collection system which will prevent problems with dangling
! aliases or incorrect sequences of destroy calls. However this level of
! support requires that a small remnant of the object is kept in memory
! past the destroy call. This can lead to an unexpected increase in memory
! consumption over the course of execution in applications that use
! temporary ESMF objects. For situations where the repeated creation and
! destruction of temporary objects leads to memory issues, it is
! recommended to call with {\tt noGarbage} set to {\tt .TRUE.}, fully
! removing the entire temporary object from memory.
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
integer :: localrc ! Error status
type (ESMF_LocStreamType), pointer :: lstypep
integer :: i
type(ESMF_Logical) :: valid
! Initialize return code; assume failure until success is certain
if (present(rc)) rc = ESMF_RC_NOT_IMPL
! Init check input types
ESMF_INIT_CHECK_DEEP_SHORT(ESMF_LocStreamGetInit,locstream,rc)
if (.not.associated(locstream%lstypep)) then
call ESMF_LogSetError(rcToCheck=ESMF_RC_OBJ_BAD, &
msg="Uninitialized or already destroyed LocStream: lstypep unassociated", &
ESMF_CONTEXT, rcToReturn=rc)
return
endif
! See if this object is even still valid in garbage collection
call c_ESMC_VMValidObject(locstream%lstypep%base, valid, localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
if (valid/=ESMF_TRUE) then
! nothing to be done here, return successfully
if (present(rc)) rc = ESMF_SUCCESS
return
endif
! Destruct all field internals and then free field memory.
call ESMF_LocStreamDestruct(locstream%lstypep, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
if (present(noGarbage)) then
if (noGarbage) then
! destroy Base object (which also removes it from garbage collection)
call ESMF_BaseDestroy(locstream%lstypep%base, noGarbage, rc=localrc)
if (ESMF_LogFoundError(localrc, &
ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! remove reference to this object from ESMF garbage collection table
call c_ESMC_VMRmFObject(locstream)
! deallocate the actual field data structure
deallocate(locstream%lstypep, stat=localrc)
if (ESMF_LogFoundDeallocError(localrc, &
msg="Deallocating LocStream information", &
ESMF_CONTEXT, rcToReturn=rc)) return
endif
endif
! Mark this locstream as invalid
nullify(locstream%lstypep)
! Set init status to indicate structure has been destroyed
ESMF_INIT_SET_DELETED(locstream)
! return successfully
if (present(rc)) rc = ESMF_SUCCESS
end subroutine ESMF_LocStreamDestroy