diff --git a/Eigen/src/SparseCore/SparseRef.h b/Eigen/src/SparseCore/SparseRef.h index a558230e7..d91f38f97 100644 --- a/Eigen/src/SparseCore/SparseRef.h +++ b/Eigen/src/SparseCore/SparseRef.h @@ -185,20 +185,27 @@ class Ref, Options, StrideType EIGEN_SPARSE_PUBLIC_INTERFACE(Ref) template - inline Ref(const SparseMatrixBase& expr) + inline Ref(const SparseMatrixBase& expr) : m_hasCopy(false) { construct(expr.derived(), typename Traits::template match::type()); } - inline Ref(const Ref& other) : Base(other) { + inline Ref(const Ref& other) : Base(other), m_hasCopy(false) { // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy } template - inline Ref(const RefBase& other) { + inline Ref(const RefBase& other) : m_hasCopy(false) { construct(other.derived(), typename Traits::template match::type()); } + ~Ref() { + if(m_hasCopy) { + TPlainObjectType* obj = reinterpret_cast(m_object_bytes); + obj->~TPlainObjectType(); + } + } + protected: template @@ -208,6 +215,7 @@ class Ref, Options, StrideType { TPlainObjectType* obj = reinterpret_cast(m_object_bytes); ::new (obj) TPlainObjectType(expr); + m_hasCopy = true; Base::construct(*obj); } else @@ -221,11 +229,13 @@ class Ref, Options, StrideType { TPlainObjectType* obj = reinterpret_cast(m_object_bytes); ::new (obj) TPlainObjectType(expr); + m_hasCopy = true; Base::construct(*obj); } protected: char m_object_bytes[sizeof(TPlainObjectType)]; + bool m_hasCopy; }; @@ -293,20 +303,27 @@ class Ref, Options, StrideType EIGEN_SPARSE_PUBLIC_INTERFACE(Ref) template - inline Ref(const SparseMatrixBase& expr) + inline Ref(const SparseMatrixBase& expr) : m_hasCopy(false) { construct(expr.derived(), typename Traits::template match::type()); } - inline Ref(const Ref& other) : Base(other) { + inline Ref(const Ref& other) : Base(other), m_hasCopy(false) { // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy } template - inline Ref(const RefBase& other) { + inline Ref(const RefBase& other) : m_hasCopy(false) { construct(other.derived(), typename Traits::template match::type()); } + ~Ref() { + if(m_hasCopy) { + TPlainObjectType* obj = reinterpret_cast(m_object_bytes); + obj->~TPlainObjectType(); + } + } + protected: template @@ -320,11 +337,13 @@ class Ref, Options, StrideType { TPlainObjectType* obj = reinterpret_cast(m_object_bytes); ::new (obj) TPlainObjectType(expr); + m_hasCopy = true; Base::construct(*obj); } protected: char m_object_bytes[sizeof(TPlainObjectType)]; + bool m_hasCopy; }; namespace internal {