diff options
author | Gael Guennebaud <g.gael@free.fr> | 2016-12-05 16:59:30 +0100 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2016-12-05 16:59:30 +0100 |
commit | a6b971e291e9eb980eb94fa7d701f7b757dbcbd0 (patch) | |
tree | 6d255678afdab2d20193098c54ef884afefbbb32 /Eigen | |
parent | 8640ffac65fd42af0e629c60046d2cd609876a75 (diff) |
Fix memory leak in Ref<Sparse>
Diffstat (limited to 'Eigen')
-rw-r--r-- | Eigen/src/SparseCore/SparseRef.h | 31 |
1 files changed, 25 insertions, 6 deletions
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<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType EIGEN_SPARSE_PUBLIC_INTERFACE(Ref) template<typename Derived> - inline Ref(const SparseMatrixBase<Derived>& expr) + inline Ref(const SparseMatrixBase<Derived>& expr) : m_hasCopy(false) { construct(expr.derived(), typename Traits::template match<Derived>::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<typename OtherRef> - inline Ref(const RefBase<OtherRef>& other) { + inline Ref(const RefBase<OtherRef>& other) : m_hasCopy(false) { construct(other.derived(), typename Traits::template match<OtherRef>::type()); } + ~Ref() { + if(m_hasCopy) { + TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(m_object_bytes); + obj->~TPlainObjectType(); + } + } + protected: template<typename Expression> @@ -208,6 +215,7 @@ class Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType { TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(m_object_bytes); ::new (obj) TPlainObjectType(expr); + m_hasCopy = true; Base::construct(*obj); } else @@ -221,11 +229,13 @@ class Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType { TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(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<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType EIGEN_SPARSE_PUBLIC_INTERFACE(Ref) template<typename Derived> - inline Ref(const SparseMatrixBase<Derived>& expr) + inline Ref(const SparseMatrixBase<Derived>& expr) : m_hasCopy(false) { construct(expr.derived(), typename Traits::template match<Derived>::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<typename OtherRef> - inline Ref(const RefBase<OtherRef>& other) { + inline Ref(const RefBase<OtherRef>& other) : m_hasCopy(false) { construct(other.derived(), typename Traits::template match<OtherRef>::type()); } + ~Ref() { + if(m_hasCopy) { + TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(m_object_bytes); + obj->~TPlainObjectType(); + } + } + protected: template<typename Expression> @@ -320,11 +337,13 @@ class Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType { TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(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 { |