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 6#include <cstddef> 7#include <cstdlib> 8#include <new> 9 10struct A { 11 A(); 12 ~A(); 13}; 14 15struct B { 16 B (A); 17}; 18 19void foo (B*); 20 21int newed, created; 22 23int main () 24{ 25 newed = 0; // The libraries might call new before main starts. 26 try { 27 foo (new B (A ())); 28 } catch (...) { } 29 30 return !(!newed && !created); 31} 32 33A::A() { created = 1; } 34A::~A() { created = 0; } 35B::B(A) { throw 1; } 36void foo (B*) { } 37 38void* operator new (size_t size) 39#if __cplusplus <= 199711L 40 throw (std::bad_alloc) 41#endif 42{ 43 ++newed; 44 return (void *) std::malloc (size); 45} 46 47void operator delete (void *p) throw () 48{ 49 --newed; 50 free (p); 51} 52 53