aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/ctorleak.cpp
blob: 145d91be468b901119f4979781f86650d18d5eb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "main.h"

#include <exception>  // std::exception

struct Foo
{
  static unsigned object_count;
  static unsigned 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(); }
#endif
    ++Foo::object_count;
  }

  ~Foo()
  {
    --Foo::object_count;
  }

  class Fail : public std::exception {};
};

unsigned Foo::object_count = 0;
unsigned Foo::object_limit = 0;


void test_ctorleak()
{
  typedef DenseIndex Index;
  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);
#ifdef EIGEN_EXCEPTIONS
    try
    {
#endif
      Matrix<Foo, Dynamic, Dynamic> m(rows, cols);
#ifdef EIGEN_EXCEPTIONS
      VERIFY(false);  // not reached if exceptions are enabled
    }
    catch (const Foo::Fail&) { /* ignore */ }
#endif
  }
  VERIFY_IS_EQUAL(static_cast<unsigned>(0), Foo::object_count);
}