Created
August 1, 2014 20:54
-
-
Save FlyingJester/43a66afeea66d68be855 to your computer and use it in GitHub Desktop.
Avoidig leaks when using setjmp/longjmp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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