console application hanging

Posted on 2011-05-04
Last Modified: 2012-05-11
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.
Question by:olmuser
    LVL 16

    Accepted Solution

    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?
    LVL 1

    Author Comment

    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?
    LVL 28

    Assisted Solution

    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.
    LVL 6

    Expert Comment

    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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now