#include <iostream>
using namespace std;
struct Foo
{
Foo(bool should_throw) {
if(should_throw)
throw std::logic_error("Constructor failed");
cout << "Constructed at " << this << endl;
}
~Foo() {
cout << "Destroyed at " << this << endl;
}
};
void double_free_anyway()
{
Foo f(false);
f.~Foo();
new (&f) Foo(true);
}
int main() {
try {
double_free_anyway();
} catch(std::logic_error& e) {
cout << "Error: " << e.what();
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IEZvbwp7CglGb28oYm9vbCBzaG91bGRfdGhyb3cpIHsKCQlpZihzaG91bGRfdGhyb3cpCgkJCXRocm93IHN0ZDo6bG9naWNfZXJyb3IoIkNvbnN0cnVjdG9yIGZhaWxlZCIpOwoJCWNvdXQgPDwgIkNvbnN0cnVjdGVkIGF0ICIgPDwgdGhpcyA8PCBlbmRsOwoJfQoJfkZvbygpIHsKCQljb3V0IDw8ICJEZXN0cm95ZWQgYXQgIiA8PCB0aGlzIDw8IGVuZGw7Cgl9Cn07Cgp2b2lkIGRvdWJsZV9mcmVlX2FueXdheSgpCnsKCUZvbyBmKGZhbHNlKTsKCWYufkZvbygpOwoJbmV3ICgmZikgRm9vKHRydWUpOwp9CgppbnQgbWFpbigpIHsKCXRyeSB7CgkJZG91YmxlX2ZyZWVfYW55d2F5KCk7Cgl9IGNhdGNoKHN0ZDo6bG9naWNfZXJyb3ImIGUpIHsKCQljb3V0IDw8ICJFcnJvcjogIiA8PCBlLndoYXQoKTsKCX0KfQ==