Skip to main content
gamedev.net
🔒 Locked

Executing code after console is closed

Started by DividedByZero 3 replies 700+ views
Original Post
DividedByZero
DividedByZero
Is there any way to execute code after a console app has been closed, to perform clean-ups and the likes?
phresnel
phresnel
Not after closing, of course. Make sure that the control flow of your program is clean (RAII for example, in c++).

You could write a signal handler in case of a SIGTERM (though I don't know the details on windows).
SiCrane
SiCrane
This is platform dependent. For example, on Windows, by default, closing the console window with the close window button will cause the program to terminate immediately via ExitProcess(), without unwinding the stack (meaning RAII won't handle cleanup for you) or calling any of the atexit handlers. You can, however, register a handler routine with SetConsoleCtrlHandler() to process CTRL_CLOSE_EVENT to perform actions when the console is closed.

phresnel
phresnel
Quote:
Original post by SiCrane
(meaning RAII won't handle cleanup for you)


Oh I didn't mean it like that. More completly I wanted to say "If the OS decided to kill your app immediately (like with a unix SIGKILL), you can't do anything. Just ensure to have a clean CFG and proper signal handlers for a quiet conscience".

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.