From 9b17656fb75c7d20609c799d31223e5e36711051 Mon Sep 17 00:00:00 2001 From: Ted Turocy Date: Tue, 16 Apr 2024 11:04:31 +0100 Subject: [PATCH] Add missing DVector::operator=. DVector was missing the assignment operator with RHS of another DVector. As a result, the default operator was being used, which resulted in an incorrect copying of the pointers to the internally-allocated storage, and therefore a memory leak. --- src/core/dvector.h | 22 ++++++++++++++++++++-- src/core/dvector.imp | 16 +++++++++------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/core/dvector.h b/src/core/dvector.h index b061fde0c..de52deed2 100644 --- a/src/core/dvector.h +++ b/src/core/dvector.h @@ -36,14 +36,32 @@ template class DVector : public PVector { Array dvlen, dvidx; public: - explicit DVector(const PVector &sig); + explicit DVector(const PVector &shape); DVector(const DVector &v); ~DVector() override; T &operator()(int a, int b, int c); const T &operator()(int a, int b, int c) const; - DVector &operator=(T c); + DVector &operator=(const DVector &v) + { + if (this == &v) { + return *this; + } + if (dvlen != v.dvlen || dvidx != v.dvidx) { + throw DimensionException(); + } + static_cast &>(*this) = static_cast &>(v); + return *this; + } + + DVector &operator=(const Vector &v) + { + static_cast &>(*this) = v; + return *this; + } + + DVector &operator=(const T &c); }; } // end namespace Gambit diff --git a/src/core/dvector.imp b/src/core/dvector.imp index 1bf790e29..d033d9c6f 100644 --- a/src/core/dvector.imp +++ b/src/core/dvector.imp @@ -31,7 +31,7 @@ namespace Gambit { template void DVector::setindex() { int index = 1; - + for (int i = 1; i <= dvlen.Length(); i++) { dvptr[i] = this->svptr + index - 1; dvidx[i] = index; @@ -43,15 +43,17 @@ template void DVector::setindex() // DVector: Constructors, destructor, and constructive operators //-------------------------------------------------------------------------- -template DVector::DVector(const PVector &sig) - : PVector((Array)sig), dvlen(sig.Lengths().Length()), - dvidx(sig.Lengths().Length()) +template +DVector::DVector(const PVector &shape) + : PVector(static_cast &>(shape)), + dvlen(shape.Lengths().Length()), dvidx(shape.Lengths().Length()) { dvptr = new T **[dvlen.Length()]; dvptr -= 1; - for (int i = 1; i <= dvlen.Length(); i++) - dvlen[i] = sig.Lengths()[i]; + for (int i = 1; i <= dvlen.Length(); i++) { + dvlen[i] = shape.Lengths()[i]; + } setindex(); } @@ -70,7 +72,7 @@ template DVector::~DVector() if (dvptr) delete [] (dvptr + 1); } -template DVector &DVector::operator=(T c) +template DVector &DVector::operator=(const T &c) { PVector::operator=(c); return *this;