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.
This commit is contained in:
Mark Olesen
2020-07-27 10:15:03 +02:00
parent fa71840d8b
commit 65d640e58e
2 changed files with 12 additions and 2 deletions

View File

@ -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

View File

@ -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;
}