Consider the following test code:
for (int i = 0; i < 100; ++i) {
GLuint fboid = 0;
GLuint colortex = 0;
GLuint depthtex = 0;
// create framebuffer & textures
glGenFramebuffers(1, &fboid);
glGenTextures(1, &colortex);
glGenTextures(1, &depthtex);
glBindTexture(GL_TEXTURE_2D, colortex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4000, 4000, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, depthtex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 4000, 4000, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0);
glBindFramebuffer(GL_FRAMEBUFFER, fboid);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colortex, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthtex, 0);
assert(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER));
// clear it
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
// delete everything
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteFramebuffers(1, &fboid);
glDeleteTextures(1, &colortex);
glDeleteTextures(1, &depthtex);
}
// put breakpoint here
You will see in the activity monitor that "Memory used" at the bottom goes sky high (14 GB). As if the GPU was still referencing the already released textures.
I tried the following:
- call glFlush() at various places
- call glFinish() at various places
- change the order of texture/fbo deletions
- detach attachments from fbo before deletion
- call [context flushBuffer];
None of these had any effect. However (!) if I remove the glClear() call, then the problem vanishes.
What might cause this? It is also reproable on Windows, and with another implementation (which unfortunately I can't share and is a lot more complex anyway).
Have any of you seen a memory leak problem like this?
UPDATE: it is pretty obvious now, that the depth/stencil buffer is leaking. If I create a depth-only attachment, then the issue vanishes again!
UPDATE: easier to repro with Intel cards. On my late 2011 mbpro the code runs ok with the discrete card (Radeon 6750M), but produces the described leak with the integrated card (HD 3000).
UPDATE: it has been fixed on High Sierra (10.13.x)