ENH: avoid change when setting UPtrList twice (issue #1035)

UPtrList::set(const label i, T* ptr);

No-op if the new pointer value is identical to the current content.
This avoid memory management issues.
This commit is contained in:
Mark Olesen 2018-10-09 08:27:50 +02:00
parent d2b32fcc84
commit 2cd2732fed
4 changed files with 17 additions and 1 deletions

View File

@ -328,6 +328,17 @@ int main(int argc, char *argv[])
<<"addresses:" << nl;
printAddr(Info, list1);
printAddr(Info, list1a);
Info<<"values:" << nl;
print(Info, list1a);
// This should not cause problems (ie, no deletion)
{
auto* ptr = &(list1a.first());
list1a.set(0, ptr);
Info<<"values:" << nl;
print(Info, list1a);
}
PtrList<Scalar> list1b(list1a, true);

View File

@ -154,6 +154,7 @@ public:
inline bool set(const label i) const;
//- Set element to given pointer and return old element (can be null)
// No-op if the new pointer value is identical to the current content.
inline autoPtr<T> set(label i, T* ptr);
//- Set element to given autoPtr and return old element

View File

@ -182,7 +182,7 @@ public:
//- Set element to specified pointer and return the old list element,
//- which can be a nullptr.
// No checks on new element
// No-op if the new pointer value is identical to the current content.
inline T* set(const label i, T* ptr);
//- Reorder elements. Reordering must be unique (ie, shuffle).

View File

@ -180,6 +180,10 @@ template<class T>
inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
{
T* old = ptrs_[i];
if (old == ptr)
{
return nullptr; // Content did not change
}
ptrs_[i] = ptr;
return old;
}