glTexImage2D acces violation

1

Ive already posted this question, but it did not get any usefull answers. I'm trying to create two textures in OpenGL. Initially i did it like this:

unsigned int texture1, texture2;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("C:\\Users\\A\\Desktop\\wall.jpg", &width, &height, &nrChannels, 0);
if (data)
{
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
    std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
data = stbi_load("C:\\Users\\A\\Desktop\\awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
    std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

This code works perfectly fine and runs with no visible issues. Obviously building these textures are the exact same procces both times, and so i figured I would use a for loop instead:

unsigned int textures[2];
int width, height, nrChannels;
unsigned char *data = stbi_load("C:\\Users\\A\\Desktop\\wall.jpg", &width, &height, &nrChannels, 0);
glGenTextures(2, textures);
for (int i = 0; i < 2; i++)
{
    if (i == 1)
    {
        stbi_image_free(data);
        data = stbi_load("C:\\Users\\A\\Desktop\\awesomeface.png", &width, &height, &nrChannels, 0);
    }
    glBindTexture(GL_TEXTURE_2D, (i == 0) ? textures[0] : textures[1]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    if (data)
    {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, (i = 0) ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else
    {
        std::cout << "Failed to load texture!" << std::endl;
    }
}

But this code breaks completely with the error: Unhandled exception at 0x03C78893 (ig9icd32.dll) in LearnOpenGL.exe: 0xC0000005: Access violation reading location 0x0A47B000.

I am sure you can point out lots of things that you dont like about the code, or stuff that maybe could become a problem later on, but seeing as i really just want an answer for this specific problem, please dont post an answer unless you have something that will actually make this code compile.

c++
opengl
memory
textures
asked on Stack Overflow Sep 22, 2018 by Behar

1 Answer

3

The expression (i = 0) is an assignment in

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, (i = 0) ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, data);

This causes that the control variable i of the for loop, is continuously set to 0 and the loop never terminates.

You have to use the compare operator == instead of the assignment operator =:

(i == 0) ? GL_RGB : GL_RGBA

Note, the expression

(i = 0) ? GL_RGB : GL_RGBA

is evaluated as

0 ? GL_RGB : GL_RGBA

which results in

GL_RGBA

The first image is an image with 3 color channels, but because of the wrong operator you try to read 4 color channels and access the data buffer out of bounds. This causes the access violation when reading the data:

0xC0000005: Access violation reading location 0x0A47B000.

answered on Stack Overflow Sep 22, 2018 by Rabbid76 • edited Sep 22, 2018 by Rabbid76

User contributions licensed under CC BY-SA 3.0