OpenGL Memory Overwrite

Started by
7 comments, last by 21st Century Moose 13 years, 6 months ago
Hi there,

I have an image struct, like this:

// image structstruct 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?
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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
    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 );
So what are the values of width and height, and how do they relate to the data pointer?
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...
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.
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.
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 :)
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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement