1 // { dg-do run  }
2 // Test that a throw in foo destroys the A, but does not free the memory.
3 
4 #include <cstddef>
5 #include <cstdlib>
6 #include <new>
7 
8 struct A {
9   A();
10   ~A();
11 };
12 
13 struct B {
14   B (A);
15 };
16 
17 void foo (B*);
18 
19 int newed, created;
20 
main()21 int main ()
22 {
23   try {
24     foo (new B (A ()));
25   } catch (...) { }
26 
27   return !(newed && !created);
28 }
29 
A()30 A::A() { created = 1; }
~A()31 A::~A() { created = 0; }
B(A)32 B::B(A) { }
foo(B *)33 void foo (B*) { throw 1; }
34 
operator new(size_t size)35 void* operator new (size_t size) throw (std::bad_alloc)
36 {
37   ++newed;
38   return (void *) std::malloc (size);
39 }
40 
operator delete(void * p)41 void operator delete (void *p) throw ()
42 {
43   --newed;
44   std::free (p);
45 }
46 
47 
48 
49 
50