Handmade Hero » Forums » Code » setjmp/longjmp
NelsonMandella
37 posts
#15630 setjmp/longjmp
2 months, 3 weeks ago Edited by NelsonMandella on June 27, 2018, 2:31 a.m. Reason: Initial post

I'm looking for advice/opinions/wisdom on wether or not it makes sense to use the somewhat obscure C feature setjmp/longjmp in a commercial product. I currently use it to jump out of a failed assert and back to the beginning of the main loop where it then makes a graceful exit after displaying an error message.
mmozeiko
Mārtiņš Možeiko
1789 posts / 1 project
#15631 setjmp/longjmp
2 months, 3 weeks ago

If it's performance sensitive code - don't use it. They are expensive operations. It it happens rarely (call to setjmp) or like once a frame, then it does not matter, use it.

Be careful if you put this functionality in API though. Because they do not play well with C++ code. They will skip calls to destructors when doing long-jmp.

If its just for showing error message and exiting, then what's wrong with function that will will print/display error and call ExitProcess (or whatever API is available for your platform)?
NelsonMandella
37 posts
#15632 setjmp/longjmp
2 months, 3 weeks ago

It's a game, so yeah it's performance critical but setjmp is only called once a frame.

I assume you mean print the error through a windows alert box or something and not within the actual program itself. I just think that rendering the error message within the game itself is a lot more elegant and less jarring aesthetically than closing out the game window and bringing up a windows notification box with an annoying alert sound.
mmozeiko
Mārtiņš Možeiko
1789 posts / 1 project
#15633 setjmp/longjmp
2 months, 3 weeks ago

You can do separate rendering loop in show-error-message function.
NelsonMandella
37 posts
#15640 setjmp/longjmp
2 months, 3 weeks ago

Do you mean without ever returning to the main loop? Like creating a mini main loop complete with rendering within the show error message function?
mmozeiko
Mārtiņš Možeiko
1789 posts / 1 project
#15641 setjmp/longjmp
2 months, 3 weeks ago

Yes, exactly.
ratchetfreak
396 posts
#15642 setjmp/longjmp
2 months, 3 weeks ago

NelsonMandella
Do you mean without ever returning to the main loop? Like creating a mini main loop complete with rendering within the show error message function?


It's how some GUI libraries create "modal" dialogs, they enter a new loop and discard any user event that goes to the parent window.
NelsonMandella
37 posts
#15647 setjmp/longjmp
2 months, 3 weeks ago

Very interesting. Is there any reason to favor this approach versus setjmp/longjmp (which seems to me simpler). And I assume this wouldn't work or would be much harder to pull off on OS X because of the timer callback stuff, as I also need to maintain a working mac build.
mmozeiko
Mārtiņš Možeiko
1789 posts / 1 project
#15649 setjmp/longjmp
2 months, 3 weeks ago

Not many people know about setjmp/longjmp, its a bit obscure feature. Also it does not play well across API boundaries or different languages, like C++. longjmp will skip stack unwinding - that means there will be no destructors called.