• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

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.
0
Mydeen Yussouf
Asked:
Mydeen Yussouf
2 Solutions
 
imladrisCommented:
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?
0
 
Mydeen YussoufTechnology 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?
0
 
peprCommented:
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;

or

    cout << "\n lost connection with server, exiting";
    cout.flush();

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.
0
 
theKashyapCommented:
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now