diff options
author | Christoph Hertzberg <chtz@informatik.uni-bremen.de> | 2015-04-16 13:25:20 +0200 |
---|---|---|
committer | Christoph Hertzberg <chtz@informatik.uni-bremen.de> | 2015-04-16 13:25:20 +0200 |
commit | 3be9f5c4d7c114eeaf7ce372cccc0e1a2734bac9 (patch) | |
tree | 10c9b619ee7a55b0fef3e6f12650404df78262d1 /test | |
parent | e0cff9ae0db1f37d9d463f8af09fd8298a6ccd0d (diff) |
Constructing a Matrix/Array with implicit transpose could lead to memory leaks.
Also reduced code duplication for Matrix/Array constructors
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | test/array.cpp | 2 | ||||
-rw-r--r-- | test/ctorleak.cpp | 36 |
3 files changed, 30 insertions, 9 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 54ce7fb30..ffd905c27 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -252,6 +252,7 @@ ei_add_test(vectorwiseop) ei_add_test(special_numbers) ei_add_test(rvalue_types) ei_add_test(dense_storage) +ei_add_test(ctorleak) # # ei_add_test(denseLM) diff --git a/test/array.cpp b/test/array.cpp index ad0182e10..cafcf79ab 100644 --- a/test/array.cpp +++ b/test/array.cpp @@ -22,6 +22,8 @@ template<typename ArrayType> void array(const ArrayType& m) ArrayType m1 = ArrayType::Random(rows, cols), m2 = ArrayType::Random(rows, cols), m3(rows, cols); + ArrayType m4 = m1; // copy constructor + VERIFY_IS_APPROX(m1, m4); ColVectorType cv1 = ColVectorType::Random(rows); RowVectorType rv1 = RowVectorType::Random(cols); diff --git a/test/ctorleak.cpp b/test/ctorleak.cpp index 145d91be4..c158f5e4e 100644 --- a/test/ctorleak.cpp +++ b/test/ctorleak.cpp @@ -4,48 +4,66 @@ struct Foo { - static unsigned object_count; - static unsigned object_limit; + static Index object_count; + static Index object_limit; int dummy; Foo() { #ifdef EIGEN_EXCEPTIONS // TODO: Is this the correct way to handle this? - if (Foo::object_count > Foo::object_limit) { throw Foo::Fail(); } + if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); } #endif + std::cout << '+'; ++Foo::object_count; } ~Foo() { + std::cout << '-'; --Foo::object_count; } class Fail : public std::exception {}; }; -unsigned Foo::object_count = 0; -unsigned Foo::object_limit = 0; +Index Foo::object_count = 0; +Index Foo::object_limit = 0; +#undef EIGEN_TEST_MAX_SIZE +#define EIGEN_TEST_MAX_SIZE 3 void test_ctorleak() { - typedef DenseIndex Index; + typedef Matrix<Foo, Dynamic, Dynamic> MatrixX; + typedef Matrix<Foo, Dynamic, 1> VectorX; Foo::object_count = 0; for(int i = 0; i < g_repeat; i++) { Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE); - Foo::object_limit = internal::random(0, rows*cols - 2); + Foo::object_limit = internal::random<Index>(0, rows*cols - 2); + std::cout << "object_limit =" << Foo::object_limit << std::endl; #ifdef EIGEN_EXCEPTIONS try { #endif - Matrix<Foo, Dynamic, Dynamic> m(rows, cols); + std::cout << "\nMatrixX m(" << rows << ", " << cols << ");\n"; + MatrixX m(rows, cols); #ifdef EIGEN_EXCEPTIONS VERIFY(false); // not reached if exceptions are enabled } catch (const Foo::Fail&) { /* ignore */ } #endif + VERIFY_IS_EQUAL(Index(0), Foo::object_count); + + { + Foo::object_limit = (rows+1)*(cols+1); + MatrixX A(rows, cols); + VERIFY_IS_EQUAL(Foo::object_count, rows*cols); + VectorX v=A.row(0); + VERIFY_IS_EQUAL(Foo::object_count, (rows+1)*cols); + v = A.col(0); + VERIFY_IS_EQUAL(Foo::object_count, rows*(cols+1)); + } + VERIFY_IS_EQUAL(Index(0), Foo::object_count); } - VERIFY_IS_EQUAL(static_cast<unsigned>(0), Foo::object_count); } |