diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index 0bd3442d69..e8a9e852b2 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -79,6 +79,9 @@ public: //- Construct copy and increment reference count inline tmp(const tmp&); + //- Construct copy transferring content of temporary if required + inline tmp(const tmp&, bool allowTransfer); + //- Destructor, delete object when reference count == 0 inline ~tmp(); diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index bfd3831692..98a947e284 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -68,6 +68,38 @@ inline Foam::tmp::tmp(const tmp& t) } +template +inline Foam::tmp::tmp(const tmp& t, bool allowTransfer) +: + isTmp_(t.isTmp_), + ptr_(t.ptr_), + ref_(t.ref_) +{ + if (isTmp_) + { + if (allowTransfer) + { + const_cast&>(t).ptr_ = 0; + } + else + { + if (ptr_) + { + ptr_->operator++(); + } + else + { + FatalErrorIn + ( + "Foam::tmp::tmp(const tmp&, bool allowTransfer)" + ) << "attempted copy of a deallocated temporary" + << abort(FatalError); + } + } + } +} + + template inline Foam::tmp::~tmp() { diff --git a/src/finiteVolume/finiteVolume/fvc/fvcMeshPhi.C b/src/finiteVolume/finiteVolume/fvc/fvcMeshPhi.C index 3b5e98aae3..f3bf760877 100644 --- a/src/finiteVolume/finiteVolume/fvc/fvcMeshPhi.C +++ b/src/finiteVolume/finiteVolume/fvc/fvcMeshPhi.C @@ -160,7 +160,7 @@ Foam::tmp Foam::fvc::absolute } else { - return tphi; + return tmp(tphi, true); } } @@ -178,7 +178,7 @@ Foam::tmp Foam::fvc::absolute } else { - return tphi; + return tmp(tphi, true); } }