| 1 |
// { dg-do run } |
| 2 |
// Test that a throw in B's constructor destroys the A and frees the memory. |
| 3 |
// Avoid use of none-overridable new/delete operators in shared |
| 4 |
// { dg-options "-static" { target *-*-mingw* } } |
| 5 |
// { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } } |
| 6 |
/* { dg-skip-if "requires hosted libstdc++ for cstdlib malloc" { ! hostedlib } } */ |
| 7 |
|
| 8 |
#include <cstddef> |
| 9 |
#include <cstdlib> |
| 10 |
#include <new> |
| 11 |
|
| 12 |
struct A { |
| 13 |
A(); |
| 14 |
~A(); |
| 15 |
}; |
| 16 |
|
| 17 |
struct B { |
| 18 |
B (A); |
| 19 |
}; |
| 20 |
|
| 21 |
void foo (B*); |
| 22 |
|
| 23 |
int newed, created; |
| 24 |
|
| 25 |
int main () |
| 26 |
{ |
| 27 |
newed = 0; // The libraries might call new before main starts. |
| 28 |
try { |
| 29 |
foo (new B (A ())); |
| 30 |
} catch (...) { } |
| 31 |
|
| 32 |
return !(!newed && !created); |
| 33 |
} |
| 34 |
|
| 35 |
A::A() { created = 1; } |
| 36 |
A::~A() { created = 0; } |
| 37 |
B::B(A) { throw 1; } |
| 38 |
void foo (B*) { } |
| 39 |
|
| 40 |
void* operator new (size_t size) |
| 41 |
#if __cplusplus <= 199711L |
| 42 |
throw (std::bad_alloc) |
| 43 |
#endif |
| 44 |
{ |
| 45 |
++newed; |
| 46 |
return (void *) std::malloc (size); |
| 47 |
} |
| 48 |
|
| 49 |
void operator delete (void *p) throw () |
| 50 |
{ |
| 51 |
--newed; |
| 52 |
free (p); |
| 53 |
} |
| 54 |
|