| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } } |
| 3 |
//Lifetime of temporaries: |
| 4 |
//egcs 2.92 performs cleanup for temporaries inside new expressions |
| 5 |
//after the new is complete, not at the end of the full expression. |
| 6 |
|
| 7 |
#include <new> |
| 8 |
#include <cstdlib> |
| 9 |
#include <cstdio> |
| 10 |
|
| 11 |
bool new_throws; |
| 12 |
bool ctor_throws; |
| 13 |
|
| 14 |
int new_done; |
| 15 |
int ctor_done; |
| 16 |
int func_done; |
| 17 |
int dtor_done; |
| 18 |
int delete_done; |
| 19 |
|
| 20 |
int count; |
| 21 |
|
| 22 |
void init() |
| 23 |
{ |
| 24 |
new_throws = ctor_throws = false; |
| 25 |
new_done = ctor_done = func_done = dtor_done = delete_done = count = 0; |
| 26 |
} |
| 27 |
|
| 28 |
struct line_error{ |
| 29 |
int line; |
| 30 |
line_error(int i):line(i){} |
| 31 |
}; |
| 32 |
|
| 33 |
#define CHECK(cond) if(!(cond))throw line_error(__LINE__); |
| 34 |
|
| 35 |
struct A{ |
| 36 |
A(int){ |
| 37 |
ctor_done = ++count; |
| 38 |
if(ctor_throws) |
| 39 |
throw 1; |
| 40 |
} |
| 41 |
A(const A&){ |
| 42 |
CHECK(false); //no copy constructors in this code |
| 43 |
} |
| 44 |
~A(){ |
| 45 |
dtor_done = ++count; |
| 46 |
} |
| 47 |
A* addr(){return this;} |
| 48 |
}; |
| 49 |
|
| 50 |
struct B{ |
| 51 |
B(A*){} |
| 52 |
void* operator new(size_t s){ |
| 53 |
new_done = ++count; |
| 54 |
if(new_throws) |
| 55 |
throw 1; |
| 56 |
return malloc(s); |
| 57 |
} |
| 58 |
void operator delete(void *){ |
| 59 |
delete_done = ++count; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
void func(B* ) |
| 64 |
{ |
| 65 |
func_done = ++count; |
| 66 |
} |
| 67 |
|
| 68 |
void test1() |
| 69 |
{ |
| 70 |
init(); |
| 71 |
try{ |
| 72 |
func(new B(A(10).addr())); |
| 73 |
}catch(int){ |
| 74 |
} |
| 75 |
CHECK(new_done==1); |
| 76 |
CHECK(ctor_done==2); |
| 77 |
CHECK(func_done==3); |
| 78 |
CHECK(dtor_done==4); |
| 79 |
CHECK(delete_done==0); |
| 80 |
} |
| 81 |
|
| 82 |
void test2() |
| 83 |
{ |
| 84 |
init(); |
| 85 |
new_throws = true; |
| 86 |
try{ |
| 87 |
func(new B(A(10).addr())); |
| 88 |
}catch(int){ |
| 89 |
} |
| 90 |
CHECK(new_done==1); |
| 91 |
CHECK(ctor_done==0); |
| 92 |
CHECK(func_done==0); |
| 93 |
CHECK(dtor_done==0); |
| 94 |
CHECK(delete_done==0); |
| 95 |
} |
| 96 |
|
| 97 |
void test3() |
| 98 |
{ |
| 99 |
init(); |
| 100 |
ctor_throws = true; |
| 101 |
try{ |
| 102 |
func(new B(A(10).addr())); |
| 103 |
}catch(int){ |
| 104 |
} |
| 105 |
CHECK(new_done==1); |
| 106 |
CHECK(ctor_done==2); |
| 107 |
CHECK(func_done==0); |
| 108 |
CHECK(dtor_done==0); |
| 109 |
CHECK(delete_done==3); |
| 110 |
} |
| 111 |
|
| 112 |
int main() |
| 113 |
{ |
| 114 |
try{ |
| 115 |
test1(); |
| 116 |
test2(); |
| 117 |
test3(); |
| 118 |
}catch(line_error e){ |
| 119 |
printf("Got error in line %d\n",e.line); |
| 120 |
return 1; |
| 121 |
} |
| 122 |
return 0; |
| 123 |
} |