mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -28,7 +28,8 @@ Class
|
|||||||
Foam::HashPtrTable
|
Foam::HashPtrTable
|
||||||
|
|
||||||
Description
|
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
|
SourceFiles
|
||||||
HashPtrTable.C
|
HashPtrTable.C
|
||||||
|
|||||||
@ -144,7 +144,16 @@ inline bool Foam::HashPtrTable<T, Key, Hash>::set
|
|||||||
T* ptr
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user