Exiting a game by closing window.

Started by
9 comments, last by Servant of the Lord 11 years, 6 months ago

[quote name='Servant of the Lord' timestamp='1349727715' post='4988099']
Yes it would try to destruct twice and have odd side effects - hopefully crashing your program, but possibly not crashing but wrecking other parts of your progam in unpredictable ways.

But unfortunately, exit() apparently doesn't unwind the stack like I initially thought.


if it exit() doesn't unwind the stack, why would the destructor be called?[/quote]
It wouldn't in this case, because exit() doesn't unwind the stack. If it did unwind the stack, the class would be destructed twice.

exit() is more complicated than I thought... [/quote]
Most certainly.

It turns out that return 0 is more favorable than exit(0)
http://stackoverflow...vs-exit-in-main
[/quote]
Definitely - it's always preferable to do things "the regular way" then to do things some other way that has a bunch of asterisks all over it.

Functions should return by calling 'return' or in void functions, it's also fine to just reach the end of the function though you can still call 'return' if you like). int main() is no exception. main() should exit by calling "return 0" or "return EXIT_SUCCESS" on success, or return something else on failure (preferably EXIT_FAILURE).

If you enter a function, you should exit the function properly by returning. If a problem occurs, then it's permissible to exit a function through some other method (like throwing an exception, or terminating, or etc...), but each "other method" has their own select group of things you need to be aware of when using it (whether it be throwing an exception, or calling something like exit()).
Doing anything abnormal should be avoided, except when you have to when something goes wrong. Exiting a function through any method except returning is abnormal. Even when something does go wrong, try to exit normally (or recover) if you can.

This topic is closed to new replies.

Advertisement