From 41315845c28b8b62ed0c52315272102ba938c605 Mon Sep 17 00:00:00 2001 From: mjreno Date: Fri, 29 Nov 2024 07:30:57 -0500 Subject: [PATCH] fix(utils): address memory leaks in CharacterStringType, Idm (#1947) * fix memleak in CharacterStringType * call ts/tas manager deallocate in idm loader * CharacterStringType cleanup not based on fortran final --- src/Utilities/CharString.f90 | 6 ++++++ src/Utilities/Idm/mf6blockfile/Mf6FileGridInput.f90 | 4 ++++ src/Utilities/Idm/mf6blockfile/Mf6FileListInput.f90 | 6 ++++++ src/Utilities/Idm/mf6blockfile/StructArray.f90 | 2 ++ src/Utilities/Memory/Memory.f90 | 8 +++++++- src/Utilities/Memory/MemoryManager.f90 | 6 ++++++ 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Utilities/CharString.f90 b/src/Utilities/CharString.f90 index 14e2d8b395b..3e84ddcec48 100644 --- a/src/Utilities/CharString.f90 +++ b/src/Utilities/CharString.f90 @@ -31,6 +31,7 @@ module CharacterStringModule procedure :: charstring_eq_charstring procedure :: write_unformatted procedure :: strlen + procedure :: destroy generic :: assignment(=) => assign_to_charstring, assign_from_charstring generic :: operator(==) => character_eq_charstring, & charstring_eq_character, & @@ -127,4 +128,9 @@ function strlen(this) result(length) end if end function strlen + subroutine destroy(this) + class(CharacterStringType), intent(inout) :: this + if (allocated(this%charstring)) deallocate (this%charstring) + end subroutine destroy + end module CharacterStringModule diff --git a/src/Utilities/Idm/mf6blockfile/Mf6FileGridInput.f90 b/src/Utilities/Idm/mf6blockfile/Mf6FileGridInput.f90 index 8d386cfb3ab..5ceb05cf97b 100644 --- a/src/Utilities/Idm/mf6blockfile/Mf6FileGridInput.f90 +++ b/src/Utilities/Idm/mf6blockfile/Mf6FileGridInput.f90 @@ -219,7 +219,11 @@ end subroutine bndgrid_rp subroutine bndgrid_destroy(this) class(BoundGridInputType), intent(inout) :: this !< Mf6FileGridInputType + ! + ! deallocate tasmanager + call this%tasmanager%da() deallocate (this%tasmanager) + nullify (this%tasmanager) end subroutine bndgrid_destroy subroutine bndgrid_reset(this) diff --git a/src/Utilities/Idm/mf6blockfile/Mf6FileListInput.f90 b/src/Utilities/Idm/mf6blockfile/Mf6FileListInput.f90 index 6a8666e6160..e3906f1d3f5 100644 --- a/src/Utilities/Idm/mf6blockfile/Mf6FileListInput.f90 +++ b/src/Utilities/Idm/mf6blockfile/Mf6FileListInput.f90 @@ -169,7 +169,13 @@ end subroutine bndlist_rp subroutine bndlist_destroy(this) class(BoundListInputType), intent(inout) :: this !< BoundListInputType + ! + ! deallocate tsmanager + call this%tsmanager%da() deallocate (this%tsmanager) + nullify (this%tsmanager) + ! + ! deallocate StructArray call destructStructArray(this%structarray) call this%bound_context%destroy() end subroutine bndlist_destroy diff --git a/src/Utilities/Idm/mf6blockfile/StructArray.f90 b/src/Utilities/Idm/mf6blockfile/StructArray.f90 index c9c194d19a3..c447a3da02b 100644 --- a/src/Utilities/Idm/mf6blockfile/StructArray.f90 +++ b/src/Utilities/Idm/mf6blockfile/StructArray.f90 @@ -481,6 +481,7 @@ subroutine load_deferred_vector(this, icol) this%mempath) do i = 1, this%nrow p_charstr1d(i) = this%struct_vectors(icol)%charstr1d(i) + call this%struct_vectors(icol)%charstr1d(i)%destroy() end do end if @@ -668,6 +669,7 @@ subroutine check_reallocate(this) do i = 1, this%struct_vectors(j)%size p_charstr1d(i) = this%struct_vectors(j)%charstr1d(i) + call this%struct_vectors(j)%charstr1d(i)%destroy() end do deallocate (this%struct_vectors(j)%charstr1d) diff --git a/src/Utilities/Memory/Memory.f90 b/src/Utilities/Memory/Memory.f90 index f6e75b2fbc2..5d4d6363f12 100644 --- a/src/Utilities/Memory/Memory.f90 +++ b/src/Utilities/Memory/Memory.f90 @@ -99,6 +99,7 @@ end function mt_associated subroutine mt_deallocate(this) class(MemoryType) :: this + integer(I4B) :: n if (associated(this%strsclr)) then if (this%master) deallocate (this%strsclr) @@ -156,7 +157,12 @@ subroutine mt_deallocate(this) end if if (associated(this%acharstr1d)) then - if (this%master) deallocate (this%acharstr1d) + if (this%master) then + do n = 1, size(this%acharstr1d) + call this%acharstr1d(n)%destroy() + end do + deallocate (this%acharstr1d) + end if nullify (this%acharstr1d) end if end subroutine mt_deallocate diff --git a/src/Utilities/Memory/MemoryManager.f90 b/src/Utilities/Memory/MemoryManager.f90 index 9d6cca3305d..ec67a0b24b4 100644 --- a/src/Utilities/Memory/MemoryManager.f90 +++ b/src/Utilities/Memory/MemoryManager.f90 @@ -1223,6 +1223,7 @@ subroutine reallocate_charstr1d(acharstr1d, ilen, nrow, name, mem_path) ! -- copy existing values do n = 1, nrow_old astrtemp(n) = acharstr1d(n) + call acharstr1d(n)%destroy() end do ! ! -- fill new values with missing values @@ -1242,6 +1243,7 @@ subroutine reallocate_charstr1d(acharstr1d, ilen, nrow, name, mem_path) ! -- fill the reallocated character array do n = 1, nrow acharstr1d(n) = astrtemp(n) + call astrtemp(n)%destroy() end do ! ! -- deallocate temporary storage @@ -2009,6 +2011,7 @@ subroutine deallocate_charstr1d(astr1d, name, mem_path) type(MemoryType), pointer :: mt logical(LGP) :: found type(MemoryContainerIteratorType), allocatable :: itr + integer(I4B) :: n ! -- code ! ! -- process optional variables @@ -2033,6 +2036,9 @@ subroutine deallocate_charstr1d(astr1d, name, mem_path) terminate=.TRUE.) else if (mt%master) then + do n = 1, size(astr1d) + call astr1d(n)%destroy() + end do deallocate (astr1d) else nullify (astr1d)