From 662681e213c981f9559156bb1399c04459dad98b Mon Sep 17 00:00:00 2001 From: Giovanni Bussi Date: Wed, 21 Feb 2024 23:24:09 +0100 Subject: [PATCH] Fixed bug in move constructor and added move assignment No consequence on the overall code, but since the previous implementation was wrong and leading to segmentation faults in the benchmark cltool (in v2.10) I backport the fix --- src/tools/PlumedHandle.cpp | 17 +++++++++++++++++ src/tools/PlumedHandle.h | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/tools/PlumedHandle.cpp b/src/tools/PlumedHandle.cpp index 15903e5f38..7ec2e5ce5b 100644 --- a/src/tools/PlumedHandle.cpp +++ b/src/tools/PlumedHandle.cpp @@ -99,4 +99,21 @@ void PlumedHandle::cmd(const std::string & key,const TypesafePtr & ptr) { } else plumed_error() << "should never arrive here (either one or the other should work)"; } +PlumedHandle::PlumedHandle(PlumedHandle && other) noexcept: + local(std::move(other.local)), + loaded(other.loaded) +{ + other.loaded=nullptr; +} + +PlumedHandle & PlumedHandle::operator=(PlumedHandle && other) noexcept { + if(this!=&other) { + if(loaded) plumed_finalize(plumed_v2c(loaded)); + local=std::move(other.local); + loaded=other.loaded; + other.loaded=nullptr; + } + return *this; +} + } diff --git a/src/tools/PlumedHandle.h b/src/tools/PlumedHandle.h index c0ea543249..d3e1d5d77c 100644 --- a/src/tools/PlumedHandle.h +++ b/src/tools/PlumedHandle.h @@ -77,7 +77,7 @@ class PlumedHandle : /// Used when using the current kernel in order to avoid unneeded indirections. std::unique_ptr local; /// Pointer to loaded Plumed object; - void* const loaded=nullptr; + void* loaded=nullptr; /// Constructor using the path to a kernel. /// I keep it private to avoid confusion wrt the /// similar constructor of PLMD::Plumed that accepts a string (conversion from FORTRAN). @@ -94,7 +94,9 @@ class PlumedHandle : /// I make it virtual for future extensibility, though this is not necessary now. virtual ~PlumedHandle(); /// Move constructor. - PlumedHandle(PlumedHandle &&) = default; + PlumedHandle(PlumedHandle && other) noexcept; +/// Move assignment. + PlumedHandle & operator=(PlumedHandle && other) noexcept; /// Execute cmd. void cmd(const std::string & key,const TypesafePtr & ptr=nullptr); /// Bring in the possibility to pass shape/nelem