Strange C++ Behavior

Started by
4 comments, last by Gamer Gamester 16 years, 1 month ago
I'm having some troubles with a test of some texture atlas code of mine. There's a bug, and it's reproducible, but I find the behavior very, very strange. Here's an explanation of what's happening. In the main function of my test, first I set everything up (Initiate my various classes needed, load my atlas, etc..) Then I decide to display a few various elements from the atlas. For each one I have code such as the following... 1) Call my graphic reset() function (clears the screen) 2) Translate to where I want to display the sub-image 3) Call the display( subimage ) function on my atlas 4) call my graphic update() function to refresh the screen 5) call a timeout for 5 seconds 6) Repeat back to 1 with a different subimage Now here's what's bizarre, I can do this up to six times and everything works flawlessly. But if I add a seventh round, NONE of the rounds work. I get properly sized quads, but the texture is basically gray (instead of a black letter). How can the seventh round, functions that are called LATER in the main function, affect the earlier rounds? This just seems to be a programming paradox to me! I'm doing nothing strange with the 7th round, the same subimage will display properly if I do it in an earlier round with no 7th. And if I do more than 7 there is still the problem. Why only six times? What COULD be going on here? I am extremely confused. I know I should generally post code, but I don't know in what section of code to even begin looking.... every piece seems to work fine, except for this absurd end result. Please help I am going crazy.
Advertisement
Start by posting the main code the one that is supposed to display your atlas by calling your subfunctions. Basically the code that implements those 6 steps you for each subimage.
-----------------Always look on the bright side of Life!
Here's my main() function
int main( int argc, const char* argv[] ){        // Initialize glfw... I use it to create my OpenGL window	glfwInit();        // Create a "GL_Screen", a class I made that handles most of my        // OpenGL stuff -- works properly for all previous tests I've made	GL_Screen screen;        // Set the background color of the screen	Color color;	color.r = 255;	color.g = 255;	color.b = 255;	color.a = 255;	screen.Set_Background( color );	// Create a Font with my "Font" class, a child        // of my "Atlas" class (Works fine for my previous tests of it;        // if I display the entire texture the letters are layed out properly,        // and my internal std::map has all the proper dimensions and texture        // coordinates)	Font font( screen );	font.Load_TTF( "Data/my_font.ttf" );        // Below are the steps I mentioned earlier.  I'm basically just having it        // cycle through displaying various letters, the same code for each one:	screen.Reset();	glTranslated( -100, 100, -500 );	glScaled( 2, 2, 0 );	font.Display( "L" );	screen.Update();	glfwSleep( 5.0 );	screen.Reset();	glTranslated( -100, 100, -500 );	glScaled( 2, 2, 0 );	font.Display( "T" );	screen.Update();	glfwSleep( 5.0 );	screen.Reset();	glTranslated( -100, 100, -500 );	glScaled( 2, 2, 0 );	font.Display( "A" );	screen.Update();	glfwSleep( 5.0 );	screen.Reset();	glTranslated( -100, 100, -500 );	glScaled( 2, 2, 0 );	font.Display( "b" );	screen.Update();	glfwSleep( 5.0 );	screen.Reset();	glTranslated( -100, 100, -500 );	glScaled( 2, 2, 0 );	font.Display( "e" );	screen.Update();	glfwSleep( 5.0 );	screen.Reset();	glTranslated( -100, 100, -500 );	glScaled( 2, 2, 0 );	font.Display( "a" );	screen.Update();	glfwSleep( 5.0 );        // If I comment out the remaining code below, the previous six letters        // display properly.        // If the below code gets compiled in then all previous letters as well as        // this one show up as a gray texture, sometimes with a darker gray line        // along the border... these "images" do not even exist within the texture        // (a 2-channel texture)        // Note: there is nothing wrong with trying to display "u", I can do it in one        // of the earlier 6 rounds with no 7th and it works fine.	screen.Reset();	glTranslated( -100, 100, -500 );	glScaled( 2, 2, 0 );	font.Display( "u" );	screen.Update();	glfwSleep( 5.0 );}


It makes no sense to me how adding later code in main could affect the earlier code.
Use the source tags in the future, like this:

int main(){  return 0;}


You can click edit on this post to see it's code.


I think you should try to narrow done the code to a minimum that reproduces the bug. What I mean by that, try removing various lines of code in that last block to reduce the number of lines to the minimum that reproduces the bug, that should help in narrowing down the problem.

Also, what #include s or other preprocesing directives have you got at the top of that main file.

It is a very strange bug.
-----------------Always look on the bright side of Life!
The problem is likely to be in the screen or font classes, which you haven't shown us. They work fine for previous tests, but C++ works fine for other people's programs too. It's much more likely that you've done something wrong in your classes.
Fixed it!! After a very late and annoying night I managed to fix it within an hour of waking just now... and I learned a few things:

1) Sometimes your better off going to bed at a reasonable hour and sleeping on a bug rather than stay up desperately trying to fix it.

2) Whenever your C++ program displays the strangest of bugs that seem to defy the logic of the language, check for an uninitialized variable.

The problem was in my Screen class. For efficiency, I had it keep track of the id number of the current texture. Then it would only load the texture if it wasn't already loaded. (Thus taking actual advantage of having a texture atlas). But I never initialized the current texture variable!!! I did so and now everything works great.

It had crossed my mind that an uninitialized variable might be the key. I had done something like that once before, and it was one other time that a bug left me 100% bewildered. However, in that case the results were random, different each time I ran the program. This time the results were consistent and reproducible, so I had foolishly ruled it out.

How I tracked the bug down:
I added a std::cout call for each OpenGL call my classes made, so I could read what was happening. I then quickly noticed that my textures weren't always being loaded.

Now I'll be extra tired all day but at least my code works!

This topic is closed to new replies.

Advertisement