boost::asio - prevent that Win32 console forces quit?

Hello, I have a little program that receives and sends TCP and UDP packets and I'm using boost::asio for this.

Since I wanted to keep the program rather simple I decided to make a console application (I don't need any fancy windows, just some output displayed as plain text).

Here is the rough structure of my main function:
int main()
	// initialization stuff here
		boost::asio::io_service io_service;
		// give the io_service some work here; // blocking call that only returns when all the work assigned to the io_service is done
	catch(std::exception& e){
		std::cerr << e.what() << std::endl;

	// cleanup here, as well as some file saving
	return 0;

However I found out that the program never exits correctly when the user decides to close the console (with Alt+F4 or by clicking the 'X').
And instead of returning with 0 I get this:
The thread 'Win32 Thread' (0x1f64) has exited with code 2 (0x2).
The thread 'Win32 Thread' (0x1c8c) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x1d18) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x1c58) has exited with code -1073741510 (0xc000013a).
The program '[3880] server.exe: Native' has exited with code -1073741510 (0xc000013a).

By doing some debugging I found out that he never even gets to the cleanup code section.
Some examples in the asio reference show how to detect a quit signal like that (by using boost::asio::signal_set) and I even get to the code sections of those handlers when I quit the program however the program simply exits within that code section (and it is not always the same location where he quits).

At first I thought this was because I did some bad coding but I couldn't really find anything related to this issue.
So I decided to try out the examples from the official page ->
I tested it with two of the four http examples and used a Win32 console subsystem for compiling.
The result was the same as with my program, return 0; was never reached.

And only recently I stumbled upon this thread ->
Duoas wrote:
Keep in mind that you cannot stop the console from closing -- only the user can do that by responding negatively to the Windows "End Program" dialogue that will pop-up.

Does that mean that the console will force a program quit even if there is other code/threads running? That would be the only logical explanation for the problem I'm currently having.

If yes can someone give me a hint for a possible solution for this problem? Like maybe I can somehow detach the console from the process and that way be able to do a proper quit? (FreeConsole(); didn't work, I tried)

If possible I would like to stick to a console subsystem because when I use AllocConsole(); for a windows subystem to display stuff in console, the output takes a lot longer to get displayed than with console as subsystem (maybe someone has an explanation for this too?).
Last edited on
Okay I was able to get myself a little bit more time by using Duoas suggested method with SetConsoleCtrlHandler().

But I still have some problems with this method:
1) The progam still is forced to quit, it has a little bit more time "reserved" but thats only a few seconds (I guess that time frame is usually enough to cleanup my stuff but it is annoying when I debug the application).

2) I don't know how I can pass parameters to the handler in order to access the objects that I want to cleanup (not to mention that I have quite a few objects that I want/have to cleanup which are defined in the main() function).
I helped myself by making global pointers to them and accessing those pointers in the handler function but I strongly doubt this is a clean and nice solution.
Last edited on
Topic archived. No new replies allowed.