In my application I am using extensively glTexImage2D. I copy some image of an image and render it as a texture, I do it frequently at every mouse click. I give it as a byte array for rendering. The memory is being eaten up and the swap memory is also allocated. Is it a memory leak? or is it due to the fact that glTexImage2D holds any references or anything else.
Edit:
//I allocate the memory once
GLuint texName;
texture_data = new GLubyte[width*height];
// Each time user click I repeat the following code (this code in in callback)
// Before this code the texture_data is modified to reflect the changes
glGenTextures(3, &texname);
glBindTexture(GL_TEXTURE_2D, texname);
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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,texture_data);
I hope your close requests and down voting would stop now!
Assuming you’re generating a new texture with
glGenTexturesevery time you callglTexImage2D, you are wasting memory, and leaking it if you don’t keep track of all the textures you generate.glTexImage2Dtakes the input data and stores it video card memory. The texture name that you bind before callingglTexImage2D– the one you generate withglGenTexturesis a handle to that chunk of video card memory.If your texture is large and you’re allocating new memory to store more and more copies of it every time you use it, then you will quickly run out of memory. The solution is to call
glTexImage2Donce during your application’s initialization and only callglBindTexturewhen you want to use it. If you want to change the texture itself when you click, only callglBindTextureandglTexImage2D. If your new image is the same size as the previous image, you can callglTexSubImage2Dto tell OpenGL to overwrite the old image data instead of deleting it and uploading the new one.UPDATE
In response to your new code, I’m updating my answer with a more specific answer. You’re dealing with OpenGL textures in the wrong way entirely The output of
glGenTexturesis aGLuint[]and not aStringorchar[]. For every texture you generate withglGenTextures, OpenGL gives you back a handle (as an unsigned integer) to a texture. This handle stores the state you give it withglTexParameterias well a chunk of memory on the graphics card if you give it data withglTexImage[1/2/3]D. It’s up to you to store the handle and send it new data when you want to update it. If you overwrite the handle or forget about it, the data still stays on the graphics card but you can’t access it. You’re also telling OpenGL to generate 3 textures when you only need 1.Seeing as
texture_datais of a fixed size, you can update the texture withglTexSubImage2Dinstead ofglTexImage2D. Here is your code modified to avoid the memory leak from this issue: