OpenGL: Why glTextureSubImage2D crashes? I'm trying to create single-channel (no mipmaps) texture initialized with my custom pixel data, but it crashes on glTextureSubImage2D. Any idea why?

const std::vector&lt;GLubyte&gt; myData(1025 * 1025, 0);
GLuint texture;
glCreateTextures(GL_TEXTURE_2D, 1, &amp;texture);
glTextureStorage2D(texture, 1, GL_R8, 1025, 1025);
glTextureSubImage2D(texture, 0, 0, 0, 1025, 1025, GL_RED, GL_UNSIGNED_BYTE, &amp;myData[0]);
Pixel transfer operations are governed by more than just your parameters to a pixel transfer function. They are also governed by parameters passed to the glPixelStore functions.

Of particular interest to you is GL_PACK_ALIGNMENT. This tells OpenGL what the byte alignment for each row of data is in your array. By default, this value is four, not one.

This means that OpenGL will believe that the second row of your image starts at this address: &amp;myData[1028], since 1025 is not aligned to 4 bytes. And that means it will run off the end of your allocated buffer eventually.

If you are using data with 1-byte alignment for rows, you must inform OpenGL of this by calling glPixelStorei(GL_PACK_ALIGNMENT, 1);. Note that you should not do this by default; do it only when dealing with byte aligned data.