-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathearly_exc_destroy.cpp
39 lines (37 loc) · 1.39 KB
/
early_exc_destroy.cpp
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
#include <stdio.h>
#include <boost/context/fiber.hpp>
using fiber_context = boost::context::fiber;
struct Excp {
Excp(const char *x) : x(x) {}
~Excp() { fprintf(stderr, "Destroying Excp(\"%s\").\n", x); }
const char *const x;
};
int main(void) {
// 0. fiberB is prepared but not yet resumed
fiber_context fiberB{[](fiber_context &&fiberA) {
try {
// 3. fiberB throws Excp("lambda")
throw Excp("lambda");
} catch (const Excp& exc) {
// 4. fiberB catches Excp("lambda"), resumes default fiber
fiberA = std::move(fiberA).resume();
// 8. *** ANY ACCESS TO exc HERE ACCESSES A DESTROYED OBJECT ***
fprintf(stderr, "9. Should destroy Excp(\"lambda\").\n");
// 9. Excp("main") is destroyed instead
}
// 10. fiberB terminates by resuming default fiber
return std::move(fiberA);
}};
try {
// 1. default fiber throws Excp("main")
throw Excp("main");
} catch (const Excp&) {
// 2. default fiber catches Excp("main"), enters fiberB
fiberB = std::move(fiberB).resume();
// 5. current_exception() reports Excp("lambda")
fprintf(stderr, "6. Should destroy Excp(\"main\").\n");
// 6. the current_exception() is destroyed
}
// 7. default fiber resumes fiberB to let it terminate
fiberB = std::move(fiberB).resume();
}