The console close button should not crash - look at the SetConsoleCtrlHandler function. This allows you to set a handler that is called when the user clicks the close button, which allows you to clear.
You cannot guarantee the possibility of cleaning under any circumstances; itβs easy to kill the application, preventing it from being cleaned. For example, your application will never be able to detect a user killing him through the task manager.
If it is important that the application is cleaned up under any circumstances, you can see if it has a second application that monitors the first one and cleans it if necessary.
source share