Thread - Shared Memory Access Violation

1

I use a library to retrieve data from an Image (in unsigned char *), and I use it to allocate in an OpenGL Buffer.

Because the image can be large, OpenGL by default creates another thread to perform this activity. But I realize free of that image soon after in the main thread, because the data is no longer necessary. Then I get the access violation error in the address (Visual Studio).

  • Does C ++ have any recourse to check if a memory is not being used by another Thread Implicit (ie I do not have access)?

Details:

  • The function I use, which generates a new thread is glTexImage2D.
  • The error occurs randomly, it depends a lot if the main thread runs free before Opengl allocates to the Buffer

Technical Details:

Exception generated at 0x07B8F54F (ig8icd32.dll) on ... [programname.exe]: 0xC0000005: access violation while reading location 0x13E1B000.

The address 0x13E1B000 is null. Excerpt of code as requested

int width, height, nrChannels;

unsigned char *dataImg = stbi_load(path, &width, &height, &nrChannels, 0);
GLenum format;
if (nrChannels == 1)
    format = GL_RED;
else if (nrChannels == 3)
    format = GL_RGB;
else if (nrChannels == 4)
    format = GL_RGBA;

if (dataImg) {
    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, dataImg);
    glGenerateMipmap(GL_TEXTURE_2D);
} else {
    log(StringBuilder::concat("STBI_LOAD::FAILED_TO_LOAD_IMAGE::", path));
}

stbi_image_free(dataImg);// <- ERRO AQUI 
    
asked by anonymous 09.03.2018 / 21:10

1 answer

0

Thanks for the tip in the comments. According to the OpenGL documentation , glTexImage2D may generate error during download if the image is not divisible by 4.

  

You create storage for a Texture and upload pixels to it with glTexImage2D (or similar functions, as appropriate to the type of texture). If your program crashes during the upload, or diagonal lines appear in the resulting image, this is because the alignment of each horizontal line of your pixel array is not multiple of 4. This typically happens to users loading an image that is of the RGB or BGR format (for example, 24 BPP images), depending on the source of your image data.

    
17.03.2018 / 17:11