Sign in to follow this  
british

C++ programs hangs for no reason

Recommended Posts

Hi, I am writing a program in C++ on Windows and compiling with GNU's GCC compiler. My is that my program hangs once it reaches a certain method. The reason I can't figure it out is that when I Debug it, everything works! I've had similar problems before, but I just can't get through this one. This is the code where this happens:
progress = 100 * j / numPollies;       // Compute progress

        // Vertex scores
        for (int i = 0; i < numPoints; i++)                                         // Loop through the vertices
        {
            vertexScore[i] = calcVertexScore(vertCachePos[i], numPolyWithVertex[i]);             // Get Vertex score
        }

        // Polygon scores
        for (int i = 0; i < numPollies; i++)
        {
            polyScore[i] =    vertexScore[polygon[3 * i]]                           // Polygon score is the sum of its vertex scores
                            + vertexScore[polygon[3 * i + 1]]
                            + vertexScore[polygon[3 * i + 2]];
        }

        // Find next polygon
        max = 0.0f;           // Holds current max score
        nextPoly = -1;          // Holds current next polygon

        for (int i = 0; i < numPollies; i++)    // Loop through the polygons
        {
            if (polyScore[i] > max)     // Is the current poly's score the greatest
            {
                max = polyScore[i];     // Replace the current max
                nextPoly = i;           // Store the next polygon's index
            }
        }

        // Add to top of cache
        for (int i = VERTEX_CACHE_SIZE; i > 2; i--) // Loop backwards through cache
        {
            cache[i] = cache[i - 3];                // Shift everything 3 spots towards the end
            if (cache[i] != -1) vertCachePos[cache[i]] = i;             // Update vertex cache position
        }

        for (int i = 0; i < 3; i++)                 // Loop through the 3 top spots
        {
            cache[i] = polygon[3 * nextPoly + i];   // Place the 3 vertices of the next polygon at the top
            vertCachePos[cache[i]] = i;             // Update vertex cache position
            numPolyWithVertex[cache[i]]--;                 // Decrement the number of polygons left to draw with current vert
        }


I can guarantee it's not a pointer issue (segmentation fault). As I said, I can run through it fine in Debug, it simply hangs when running on its own. Does anyone know what causes this? Thanks! [Edited by - british on August 13, 2007 7:03:28 PM]

Share this post


Link to post
Share on other sites
Shouldn't


for (int i = VERTEX_CACHE_SIZE; i > 2; i--) // Loop backwards through cache
{
cache[i] = cache[i - 3]; // Shift everything 3 spots towards the end
if (cache[i] != -1) vertCachePos[cache[i]] = i; // Update vertex cache position
}


be:


for (int i = VERTEX_CACHE_SIZE - 1; i > 2; i--) // Loop backwards through cache
{
cache[i] = cache[i - 3]; // Shift everything 3 spots towards the end
if (cache[i] != -1) vertCachePos[cache[i]] = i; // Update vertex cache position
}


Assuming cache is VERTEX_CACHE_SIZE elements large, you are stomping memory. Doubt that is the problem, however its one potential problem [smile]

Share this post


Link to post
Share on other sites
Wow good call! I can't believe I passed over that...It doesn't produce segmentation faults either.

Unfortunately my program is still hanging for no reason, but thanks for that!

Share this post


Link to post
Share on other sites
Quote:
Original post by british
Unfortunately my program is still hanging for no reason, but thanks for that!


Computers never do anything for "no reason"; they do exactly what they're told.

So... when you pause the application in the debugger, where is the code cycling?

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by british
I can guarantee it's not a pointer issue (segmentation fault).


Hate to break it to you, but pointer issues can cause just about anything to happen, not neccessarily a segmentation fault.

Contrived Example: In your last loop, numPolyWithVertex[cache[i]]--; might actually end up decrementing i, causing an infinite loop.

Use the debugger properly and you should have an answer in no time.

Share this post


Link to post
Share on other sites
You're right, there is a reason. I just found what it is. My program is doing something very long and for some reason it stops updating the screen and says the program is not responding. I found a way to speed it up so it seems to work slightly better now. I hate windows. Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by british
for some reason it stops updating the screen and says the program is not responding


I recall that you have to check the message queue, or something like that, periodically. If your program goes a certain length of time without doing so, windows marks your program as not responding, silently removes your window and replaces it with one marked (Not Responding). Basically, don't have anything remotely like a 'blocking' operation in your message loop.

That's just what I recall, check MSDN for confirmation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this