C++ programs hangs for no reason

Started by
5 comments, last by DaBookshah 16 years, 8 months ago
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 = calcVertexScore(vertCachePos, numPolyWithVertex);             // Get Vertex score
        }

        // Polygon scores
        for (int i = 0; i < numPollies; i++)
        {
            polyScore =    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 > max)     // Is the current poly's score the greatest
            {
                max = polyScore;     // 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 = cache;                <span class="cpp-comment">// Shift everything 3 spots towards the end</span>
            <span class="cpp-keyword">if</span> (cache<span style="font-weight:bold;"> != -<span class="cpp-number">1</span>) vertCachePos[cache<span style="font-weight:bold;">] = i;             <span class="cpp-comment">// Update vertex cache position</span>
        }

        <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>; i &lt; <span class="cpp-number">3</span>; i++)                 <span class="cpp-comment">// Loop through the 3 top spots</span>
        {
            cache<span style="font-weight:bold;"> = polygon[<span class="cpp-number">3</span> * nextPoly + i];   <span class="cpp-comment">// Place the 3 vertices of the next polygon at the top</span>
            vertCachePos[cache<span style="font-weight:bold;">] = i;             <span class="cpp-comment">// Update vertex cache position</span>
            numPolyWithVertex[cache<span style="font-weight:bold;">]–;                 <span class="cpp-comment">// Decrement the number of polygons left to draw with current vert</span>
        }


</pre></div><!–ENDSCRIPT–>
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 &#111;n its own.

Does anyone know what causes this?

Thanks!

<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - british on August 13, 2007 7:03:28 PM]<!–EDIT–></span><!–/EDIT–>
Advertisement
Shouldn't

for (int i = VERTEX_CACHE_SIZE; i > 2; i--) // Loop backwards through cache{cache = cache; // Shift everything 3 spots towards the end<br>if (cache<span style="font-weight:bold;"> != -1) vertCachePos[cache<span style="font-weight:bold;">] = i; // Update vertex cache position<br>}<br></pre><br><br>be:<br><br><pre><br>for (int i = VERTEX_CACHE_SIZE - 1; i &gt; 2; i–) // Loop backwards through cache<br>{<br>cache<span style="font-weight:bold;"> = cache; // Shift everything 3 spots towards the end<br>if (cache<span style="font-weight:bold;"> != -1) vertCachePos[cache<span style="font-weight:bold;">] = i; // Update vertex cache position<br>}<br></pre><br><br>Assuming cache is VERTEX_CACHE_SIZE elements large, you are stomping memory. Doubt that is the problem, however its &#111;ne potential problem [smile]
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
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!
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
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]--; might actually end up decrementing i, causing an infinite loop.

Use the debugger properly and you should have an answer in no time.
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.
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.

This topic is closed to new replies.

Advertisement