Jump to content
  • Advertisement
Sign in to follow this  
Sigvatr

OpenGL OpenGL Memory Overwrite

This topic is 2809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there,

I have an image struct, like this:

// image struct
struct image_bd_s
{
GLuint handle;
bool loaded;
unsigned int width;
unsigned int height;
};


After loading (or binding) a texture, OpenGL overwrites some of the data in the struct. The height variable is set to a ridiculously high value, which causes OpenGL to attempt to draw texturesover millions of pixels in height.

I'm not sure where this is happening. It might be during the loading of the texture, or perhaps when I bind the texture.

I tried moving the GLuint handle to the bottom of the struct. However, this simply causes the program to crash, as OpenGL is overwriting memory outside of the struct.

Anyone have any ideas why this is happening?

Share this post


Link to post
Share on other sites
Advertisement
I assume you are passing &handle to opengl somewhere, what kinds of commands are you passing it to? Post a little code so we can get an idea what you're doing with this struct.

Share this post


Link to post
Share on other sites

glGenTextures( 1, &img->handle );
glBindTexture( GL_TEXTURE_2D, img->handle );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D( GL_TEXTURE_2D, 0, type, img->width, img->height, 0, type,
GL_UNSIGNED_BYTE, data );

glBindTexture( GL_TEXTURE_2D, img->handle );

Share this post


Link to post
Share on other sites
Width and height are always powers of 2; they are used as such:

    glBegin( GL_QUADS );
glTexCoord2i( 0, 0 );
glVertex2f( x, y - img->height );
glTexCoord2i( 0, 1 );
glVertex2f( x, y );
glTexCoord2i( 1, 1 );
glVertex2f( x + img->width, y );
glTexCoord2i( 1, 0 );
glVertex2f( x + img->width, y - img->height );
glEnd();


When I retrieve img->height in the above example, it says it is 23624725 or something; it is supposed to be typically 128, 256 etc...

Share this post


Link to post
Share on other sites
Add a data breakpoint to img->height, that way you can see exactly where that memory gets overwritten. Can't tell you how to do it since you didn't specify which IDE you're using :) And if I had to guess I'd say you have to make sure your data array is large enough to hold your image data.

Share this post


Link to post
Share on other sites
I created a data buffer between the texture handle and the rest of the data in the struct. Seems to have fixed the problem, although I think I will probably lose some sleep tonight knowing that I'm wasting 40 bytes of data.

Share this post


Link to post
Share on other sites
That is probably not a good solution :)
OpenGL is not supposed to write ANYTHING outside of the 4-byte handle pointer. Step through your code and keep track of exactly where your struct is getting messed up.
If it happens after you called glGenTextures then I'm stumped and would blame the driver :)

You say it's messed up when you get to the drawing part, but what else have happened in between? In this case I would say that you yourself are causing the overwrite somewhere along the way. Without the entire code-flow it's impossible to say. But I personally would blame opengl only when all other options had been exhausted. And even at that point it would probably turn out I did something really silly that I just missed.

Debug your code :)

Share this post


Link to post
Share on other sites
I doubt if it's anything in this particular struct. From the sound of things, I'd guess that you're overflowing an array somewhere else in your code and it's stomping over the struct memory.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!