Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is the effect of Ctrl-C on C++ Win32 console applications?

  1. Is it possible to handle this event in some way?
  2. What happens in terms of stack unwinding and deallocation of static/global objects?
like image 484
Assaf Lavie Avatar asked May 27 '09 08:05

Assaf Lavie


2 Answers

Ctrl-C in console application will generate a signal. The default handler of this signal calls ExitProcess to terminate the application. You can override this behaviour by setting your own handler functions for the signal using SetConsoleCtrlHandler function.

like image 169
Shino C G Avatar answered Sep 21 '22 14:09

Shino C G


EDIT: SIGINT, not SIGTERM. And Assaf reports that no objects are destroyed (at least on Windows) for unhanded SIGINT.

The system sends a SIGINT. This concept applies (with some variance) for all C implementations. To handle it, you call signal, specifying a signal handler. See the documentation on the signal function at Open Group and MSDN.

The second question is a little trickier, and may depend on implementation. The best bet is to handle the signal, which allows you to use delete and exit() manually.

like image 30
Matthew Flaschen Avatar answered Sep 22 '22 14:09

Matthew Flaschen