Skip to content

Instantly share code, notes, and snippets.

@FlyingJester
Created August 1, 2014 20:54
Show Gist options
  • Select an option

  • Save FlyingJester/43a66afeea66d68be855 to your computer and use it in GitHub Desktop.

Select an option

Save FlyingJester/43a66afeea66d68be855 to your computer and use it in GitHub Desktop.
Avoidig leaks when using setjmp/longjmp
//g++ -std=c++11 setjmp_unique_ptr.cpp -o demo
#include <cstdio>
#include <memory>
#include <csetjmp>
struct Test {
Test(){
std::fputs("Created Test.\n", stdout);
}
~Test(){
std::fputs("Destroyed Test.\n", stdout);
}
};
int main(){
// Declare your smart pointers before the first setjmp,
// and set them where you would normally initialize them.
std::unique_ptr<Test> TestHolder(nullptr);
std::jmp_buf f;
if(setjmp(f)){return 0;}
TestHolder.reset(new Test());
longjmp(f, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment