BUG: potential memory leaks in HashPtrTable::set (#1787)

- backported fix from develop

COMP: incorrect variable names in PtrListOpsTemplates.C
This commit is contained in:
Mark Olesen
2020-07-27 10:00:58 +02:00
parent 540589fc22
commit 3455d556c4
2 changed files with 17 additions and 5 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -86,7 +86,19 @@ inline bool Foam::HashPtrTable<T, Key, Hash>::set
T* ptr T* ptr
) )
{ {
return this->parent_type::set(key, ptr); // Newer: const T* old = this->get(key);
iterator iter(this->find(key));
const T* old = (iter.good() ? iter.val() : nullptr);
const bool ok = this->parent_type::set(key, ptr);
if (ok && old != ptr)
{
delete const_cast<T*>(old);
}
return ok;
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -79,7 +79,7 @@ void Foam::sortedOrder
template<class T> template<class T>
void Foam::sort(UPtrList<T>& list) void Foam::sort(UPtrList<T>& list)
{ {
labelList order(input.size()); labelList order(list.size());
sortedOrder(list, order); sortedOrder(list, order);
list.sortOrder(order, false); // false = allow nullptr list.sortOrder(order, false); // false = allow nullptr
} }
@ -88,7 +88,7 @@ void Foam::sort(UPtrList<T>& list)
template<class T, class Compare> template<class T, class Compare>
void Foam::sort(UPtrList<T>& list, const Compare& comp) void Foam::sort(UPtrList<T>& list, const Compare& comp)
{ {
labelList order(input.size()); labelList order(list.size());
sortedOrder(list, order, comp); sortedOrder(list, order, comp);
list.sortOrder(order, false); // false = allow nullptr list.sortOrder(order, false); // false = allow nullptr
} }