0
I use a library to retrieve data from an Image (in unsigned char*), and use it to allocate to an Opengl Buffer (Texture).
Since the image can be large, Opengl by default creates another thread to perform this activity. But I do free this image right after the main thread, because the data is no longer needed. Then I get the access breach error at (Visual Studio).
- C++ has some feature for checking that one memory is not being used by another Implicit Thread (i.e., I don’t 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 in Buffer
Technical details:
Exception generated in 0x07B8F54F (ig8icd32.dll) in ...[nomedoprograma.exe]: 0xC0000005: access violation while reading the local 0x13E1B000.
The address 0x13E1B000 is null. Code snippet 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
I do not know any language solutions to this competition problem that you put to the main point of the question. But there are competing libraries that help you avoid that. Semaphores and mutex are the ones I see most in C. It might be interesting to read what Visual Studio’s C++ compiler documentation has to say about competition.
– Jefferson Quesado
The time is not conducive to draw attention, but I believe that giving a detail of the problematic code excerpt, presenting a [MCVE], can generate results when more users are active. And managing to simulate your problem too. Competition problems are heisenbugs. Helping to simulate the problem consistently helps in the solution
– Jefferson Quesado
Code added, I don’t know if it’ll help much. I wanted to make the topic broader for shared memory problems, not just specific to Opengl.
– Sveen
Where you saw that the function glTexImage2D generates a new thread, as far as I know Opengl is entirely designed to work in single Thread. Try moving the code 'stbi_image_free(dataImg);' into if (dataImg), if there is no image it makes no sense to release resource.
– paulocanedo