rlbond
rlbond

Reputation: 67829

What happens when you close a c++ console application

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?

Upvotes: 25

Views: 20575

Answers (4)

user472308
user472308

Reputation:

SIGBREAK is raised on Windows.

Upvotes: 8

Raul Agrait
Raul Agrait

Reputation: 6018

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.

Upvotes: 21

Zyx
Zyx

Reputation: 495

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.

Upvotes: 9

AndrewR
AndrewR

Reputation: 10889

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:

Upvotes: 9

Related Questions