Commit 65d640e5 authored by Mark Olesen's avatar Mark Olesen

BUG: potential memory leaks in HashPtrTable (#1787)

- using HashPtrTable::set() with the same key twice did not guarantee
  proper cleanup of memory since it simply used the underlying
  HashTable::set() without doing anything about the old memory. Now
  check for pre-existing storage and delete it when it does not
  correspond to the newly stored pointer.

  This problem is independent of potential memory slicing previously
  flagged (#1286) and only partially resolved.
parent fa71840d
......@@ -28,7 +28,8 @@ Class
Foam::HashPtrTable
Description
A HashTable of pointers to objects of type \<T\>.
A HashTable of pointers to objects of type \<T\>,
with deallocation management of the pointers.
SourceFiles
HashPtrTable.C
......
......@@ -144,7 +144,16 @@ inline bool Foam::HashPtrTable<T, Key, Hash>::set
T* ptr
)
{
return this->parent_type::set(key, ptr);
const T* old = this->get(key);
const bool ok = this->parent_type::set(key, ptr);
if (ok && old != ptr)
{
delete const_cast<T*>(old);
}
return ok;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment