1 #include "main.h"
2
3 #include <exception> // std::exception
4
5 struct Foo
6 {
7 static Index object_count;
8 static Index object_limit;
9 int dummy;
10
FooFoo11 Foo()
12 {
13 #ifdef EIGEN_EXCEPTIONS
14 // TODO: Is this the correct way to handle this?
15 if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); }
16 #endif
17 std::cout << '+';
18 ++Foo::object_count;
19 }
20
~FooFoo21 ~Foo()
22 {
23 std::cout << '-';
24 --Foo::object_count;
25 }
26
27 class Fail : public std::exception {};
28 };
29
30 Index Foo::object_count = 0;
31 Index Foo::object_limit = 0;
32
33 #undef EIGEN_TEST_MAX_SIZE
34 #define EIGEN_TEST_MAX_SIZE 3
35
test_ctorleak()36 void test_ctorleak()
37 {
38 typedef Matrix<Foo, Dynamic, Dynamic> MatrixX;
39 typedef Matrix<Foo, Dynamic, 1> VectorX;
40 Foo::object_count = 0;
41 for(int i = 0; i < g_repeat; i++) {
42 Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE);
43 Foo::object_limit = internal::random<Index>(0, rows*cols - 2);
44 std::cout << "object_limit =" << Foo::object_limit << std::endl;
45 #ifdef EIGEN_EXCEPTIONS
46 try
47 {
48 #endif
49 std::cout << "\nMatrixX m(" << rows << ", " << cols << ");\n";
50 MatrixX m(rows, cols);
51 #ifdef EIGEN_EXCEPTIONS
52 VERIFY(false); // not reached if exceptions are enabled
53 }
54 catch (const Foo::Fail&) { /* ignore */ }
55 #endif
56 VERIFY_IS_EQUAL(Index(0), Foo::object_count);
57
58 {
59 Foo::object_limit = (rows+1)*(cols+1);
60 MatrixX A(rows, cols);
61 VERIFY_IS_EQUAL(Foo::object_count, rows*cols);
62 VectorX v=A.row(0);
63 VERIFY_IS_EQUAL(Foo::object_count, (rows+1)*cols);
64 v = A.col(0);
65 VERIFY_IS_EQUAL(Foo::object_count, rows*(cols+1));
66 }
67 VERIFY_IS_EQUAL(Index(0), Foo::object_count);
68 }
69 }
70