Commit 2cd2732f authored by Mark OLESEN's avatar Mark OLESEN
Browse files

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.
parent d2b32fcc
......@@ -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);
......
......@@ -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
......
......@@ -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).
......
......@@ -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;
}
......
Supports Markdown
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