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:
// initialization stuff here
// give the io_service some work here
io_service.run(); // blocking call that only returns when all the work assigned to the io_service is done
std::cerr << e.what() << std::endl;
// cleanup here, as well as some file saving
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 ' 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 -> http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/examples.html
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,
was never reached.
And only recently I stumbled upon this thread -> http://cplusplus.com/forum/general/11433/
|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? (
didn't work, I tried)
If possible I would like to stick to a console subsystem because when I use
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?).