console application hanging

What are the typical causes that might make a console application hang after the return statement? Below are the last two statements:
	cout << "\n lost connection with server, exiting";
	return 0;

Open in new window

The cout statement gets printed and it almost like the return statement never gets executed.

I am running this in DEBUG configuration in RADstudio.
ol muserTechnology GeneralistAsked:
Who is Participating?
Well, clearly you're getting into *un*common kinds of causes there.
One I've run into on occasion is the stack getting corrupted (due to buffer overruns or something like that). That can cause the return address on the stack to be overwritten.

I'm assuming this is happening in a function? What happens when you do a single step on the return line?
ol muserTechnology GeneralistAuthor Commented:
It was just a console application. I just tried creating a VCL forms application. The application runs OK if it is just by itself. I add a few static libraries to the project. The application seems to run but the main form won't show up. So the only option I have is to stop the application from IDE as I am running it in debug mode. Those static libraries used to work before. Is my system getting corrupted or is it some known issue with RAD studio 2010?
It is likely that the lines do not cause the hang.  If the "return 0" is part of the main() body, then it is converted to calling a runtime routines that finalize the execution of the program.  Many things are to be done.  One of the things is to flush the stream buffers and close the streams (an all open files).  When observing the text printed on the console, the code pointer may actually point to the runtime code (after the "return 0" was "recognized").

Try to use

    cout << "\n lost connection with server, exiting" << endl;


    cout << "\n lost connection with server, exiting";

to get the text on console before the return.  You may also to add a kind of neutral command just before the return -- only to put the breakpoint earlier than to "return" command.

One of the problem that may cause the program hang during the exit is that the runtime also have to release the memory.  In C++ it also means to call the destructors of the still existing objects.  The problem is that sometimes the runtime may be too rough to call them correctly.

Try to put all the body of the main (before the "return 0") to the nested block (i.e. { }).  If the objects are created inside the nested block, their destructors are called automatically when the nested block ends -- i.e. earlier than in finalizing runtime code.
Did you already do step-by-step execution?
What does the call stack show after execution of "return 0;"?

PS: At least and endl should be put at the end of cout, if not flush as a good coding practice. Though I don't see how it's connected to the problem.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.