Skip to content

Commit

Permalink
fix(utils): address memory leaks in CharacterStringType, Idm (#1947)
Browse files Browse the repository at this point in the history
* fix memleak in CharacterStringType

* call ts/tas manager deallocate in idm loader

* CharacterStringType cleanup not based on fortran final
  • Loading branch information
mjreno authored Nov 29, 2024
1 parent 4c14af9 commit 4131584
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Utilities/CharString.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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, &
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/Utilities/Idm/mf6blockfile/Mf6FileGridInput.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/Utilities/Idm/mf6blockfile/Mf6FileListInput.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Utilities/Idm/mf6blockfile/StructArray.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion src/Utilities/Memory/Memory.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/Utilities/Memory/MemoryManager.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 4131584

Please sign in to comment.