Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL Memory Overwrite


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 S1G   Members   -  Reputation: 143

Like
0Likes
Like

Posted 15 October 2010 - 08:12 PM

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?

Sponsor:

#2 karwosts   Members   -  Reputation: 840

Like
0Likes
Like

Posted 15 October 2010 - 08:31 PM

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.

#3 S1G   Members   -  Reputation: 143

Like
0Likes
Like

Posted 15 October 2010 - 09:35 PM


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 );


#4 Brother Bob   Moderators   -  Reputation: 8194

Like
0Likes
Like

Posted 15 October 2010 - 09:47 PM

So what are the values of width and height, and how do they relate to the data pointer?

#5 S1G   Members   -  Reputation: 143

Like
0Likes
Like

Posted 15 October 2010 - 10:18 PM

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...

#6 BertS   Members   -  Reputation: 124

Like
0Likes
Like

Posted 15 October 2010 - 10:45 PM

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.

#7 S1G   Members   -  Reputation: 143

Like
0Likes
Like

Posted 15 October 2010 - 10:51 PM

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.

#8 panic   Members   -  Reputation: 211

Like
0Likes
Like

Posted 15 October 2010 - 11:25 PM

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 :)


#9 mhagain   Crossbones+   -  Reputation: 7969

Like
0Likes
Like

Posted 16 October 2010 - 06:07 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS