From 0b493e02bacf19e4b86ec24d0ac431e50bb44364 Mon Sep 17 00:00:00 2001 From: Sunny Titus <77051845+Manangka@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:22:07 +0200 Subject: [PATCH] fix(mem): address memory leak related to the MemoryStore (#1953) Fixes the memory leaks present in the PtrHashTableIterator and PtrHashTable --- src/Utilities/Memory/MemoryContainerIterator.f90 | 4 ++-- src/Utilities/Memory/MemoryManagerExt.f90 | 1 + src/Utilities/Memory/MemoryStore.f90 | 8 +++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Utilities/Memory/MemoryContainerIterator.f90 b/src/Utilities/Memory/MemoryContainerIterator.f90 index bad175bf602..f9169a1d35a 100644 --- a/src/Utilities/Memory/MemoryContainerIterator.f90 +++ b/src/Utilities/Memory/MemoryContainerIterator.f90 @@ -29,10 +29,10 @@ module MemoryContainerIteratorModule !! !< function constructor(container_iterator) result(iterator) - class(IteratorType) :: container_iterator + class(IteratorType), allocatable :: container_iterator type(MemoryContainerIteratorType) :: iterator - iterator%container_iterator = container_iterator + call move_alloc(container_iterator, iterator%container_iterator) end function constructor diff --git a/src/Utilities/Memory/MemoryManagerExt.f90 b/src/Utilities/Memory/MemoryManagerExt.f90 index 00d61b7ebe7..255c745e738 100644 --- a/src/Utilities/Memory/MemoryManagerExt.f90 +++ b/src/Utilities/Memory/MemoryManagerExt.f90 @@ -46,6 +46,7 @@ subroutine memorystore_remove(component, subcomponent, context) if (mt%path == memory_path .and. mt%mt_associated()) then call mt%mt_deallocate() removed = .true. + deallocate (itr) exit end if end do diff --git a/src/Utilities/Memory/MemoryStore.f90 b/src/Utilities/Memory/MemoryStore.f90 index 4a7664770af..93a7a40e6fc 100644 --- a/src/Utilities/Memory/MemoryStore.f90 +++ b/src/Utilities/Memory/MemoryStore.f90 @@ -12,7 +12,7 @@ module MemoryStoreModule type :: MemoryStoreType private - type(PtrHashTableType), private :: container + type(PtrHashTableType) :: container contains procedure :: iterator procedure :: add @@ -27,11 +27,13 @@ module MemoryStoreModule !! !< function iterator(this) result(itr) - ! -- dummy class(MemoryStoreType) :: this type(MemoryContainerIteratorType) :: itr + ! -- local + class(IteratorType), allocatable :: container_iterator - itr = MemoryContainerIteratorType(this%container%Iterator()) + allocate (container_iterator, source=this%container%iterator()) + itr = MemoryContainerIteratorType(container_iterator) end function !> @brief Add a MemoryType to the container