From 538c059aa4d02a9f1a282e8fc4b181c8eb0b4f47 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 9 Oct 2014 23:35:05 +0200 Subject: [PATCH] bug #887: fix CompressedStorage::reallocate wrt memory leaks --- Eigen/src/SparseCore/CompressedStorage.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Eigen/src/SparseCore/CompressedStorage.h b/Eigen/src/SparseCore/CompressedStorage.h index a93550340..4f3695773 100644 --- a/Eigen/src/SparseCore/CompressedStorage.h +++ b/Eigen/src/SparseCore/CompressedStorage.h @@ -204,17 +204,14 @@ class CompressedStorage inline void reallocate(size_t size) { - Scalar* newValues = new Scalar[size]; - Index* newIndices = new Index[size]; + eigen_internal_assert(size!=m_allocatedSize); + internal::scoped_array newValues(size); + internal::scoped_array newIndices(size); size_t copySize = (std::min)(size, m_size); - // copy - internal::smart_copy(m_values, m_values+copySize, newValues); - internal::smart_copy(m_indices, m_indices+copySize, newIndices); - // delete old stuff - delete[] m_values; - delete[] m_indices; - m_values = newValues; - m_indices = newIndices; + internal::smart_copy(m_values, m_values+copySize, newValues.ptr()); + internal::smart_copy(m_indices, m_indices+copySize, newIndices.ptr()); + std::swap(m_values,newValues.ptr()); + std::swap(m_indices,newIndices.ptr()); m_allocatedSize = size; }