25

I guess the question says it all, but, what happens if someone closes a c++ console app? As in, clicks the "x" in the top corner. Does it instantly close? Does it throw some sort of exception? Is it undefined behavior?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • 2
    What platform are you interested in? Windows? Linux? That really affects the answer. – Rob K Mar 30 '09 at 12:36
  • The use of the phrase "console app" implies windows, as that's MS terminology for character mode apps. – AndrewR Apr 01 '09 at 02:29

4 Answers4

21

Closing a c++ console app with the "x" in the top corner throws an CTRL_CLOSE_EVENT which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function. In there you could override the close functionality and perform whatever you wished to do, and then optionally still perform the default behavior.

Raul Agrait
  • 5,938
  • 6
  • 49
  • 72
  • You can't override closing the console. The process exits whether you return `TRUE` or `FALSE`. If you delay instead or returning, e.g. to do cleanup, you're given 5 seconds by default before your process is killed. It's the same even if you manually called `AllocConsole` or `AttachConsole`, and it's too late to call `FreeConsole` in the handler. If the user closing the console shouldn't terminate the main process, then you need to use a child process for the console and connect standard I/O with pipes. – Eryk Sun Aug 14 '17 at 02:56
9

I imagine that the console process just gets unceremoniously killed by the OS. If you want to trap this event and do something it looks like the SetConsoleCtrlHandler function is the way to do it.

See also:

Community
  • 1
  • 1
AndrewR
  • 10,759
  • 10
  • 45
  • 56
9

On Linux and other Unix systems, the console runs as a separate process. As you close the shell, it sends the SIGHUP signal to the currently active process or processes that are not executed in the background. If the programmer does not handle it, the process simply terminates. The same signal is sent if you close the SSH session with a terminal and an active process.

Zyx
  • 485
  • 3
  • 8
8

SIGBREAK is raised on Windows.

  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – DLeh Dec 15 '14 at 13:33
  • 3
    Sure it does. It is the answer I wanted when I found this page. –  Dec 15 '14 at 14:09
  • `SIGBREAK` is only raised if you're using the C runtime. Windows itself doesn't have Unix signals. The C runtime has a console control handler that maps `CTRL_C_EVENT` to `SIGINT` and all other control events to `SIGBREAK`. – Eryk Sun Aug 14 '17 at 02:59
  • @eryksun A good point. But if you *are* using the C runtime then processing `SIGBREAK` with https://www.tutorialspoint.com/cplusplus/cpp_signal_handling.htm is a clean way of dealing with a manual window closure. – DrMcCleod Jan 23 '19 at 14:45
  • @DrMcCleod, if we're using other implementations of C/C++, they may not implement `SIGBREAK` (it's not standard C) or map `CTRL_CLOSE_EVENT` to a C signal. Probably MinGW g++ has it because it steals the private C runtime of Windows (msvcrt.dll). I haven't thoroughly investigated how POSIX applications (Cygwin, MSYS) implement this when run from a console (as opposed to a terminal emulator), but when I close the console of an MSYS bash process, it immediately exits with the status `STATUS_CONTROL_C_EXIT` (0xC000013A), which suggests the default control handler gets called. – Eryk Sun Jan 23 '19 at 20:57