OS X glGenTextures texture name reuse/collision

I'm encountering some strange behavior on OS X that I cannot seem to figure out. Users have reported the manifestation of this bug on various hardware configurations on OS X 10.10 and 10.11 at least.


In my app, I am creating/deleting textures on an as-needed basis.


For some reason, under some conditions, glGenTextures returns me the same texture name multiple times, even though that name is still in use and has not been freed by glDeleteTextures.


Here's a sample log illustrating the problem. Each line shows the operation (delete or generate), the current context (just to make sure I'm dealing in the same context), a string associated with my texture object, and the texture name it is given by glGenTextures.


  • 2015-11-17 10:06:27.387 MyApp[46731:8046878] deleting 0x1018e7200 Moore 65
  • 2015-11-17 10:06:27.388 MyApp[46731:8046878] generate 0x1018e7200 Gary's 65
  • 2015-11-17 10:06:27.513 MyApp[46731:8046878] generate 0x1018e7200 Geronimo 65


So the first line, a texture "Moore" with name 65 is deleted. Then 1 ms later, a new texture is generated, and OpenGL re-uses that texture name 65 again. That's all fine and dandy. But then the very next call to glGenTextures returns 65 again, which is assigned to a different object and now I have 2 objects that own texture name 65 and things go off the rails from there.


All this happens on the main thread, and all this happens in a single context.


The code that generates and binds my texture follows:


if ((cgl_ctx = CGLGetCurrentContext ())) {

glBindTexture(GL_TEXTURE_2D, 0);

glGenTextures (1, &texName);

NSLog(@"texturing %p %@ %d", cgl_ctx, string.string, texName);

glBindTexture (GL_TEXTURE_2D, texName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSize.width, texSize.height, 0, [bitmap hasAlpha] ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, [bitmap bitmapData]);

}


The code in my class that deletes the texture looks like:


if (texName && (cgl_ctx == CGLGetCurrentContext())) {

NSLog(@"deleting %p %@ %d", cgl_ctx, string.string, self.texName);

glDeleteTextures(1, &texName);

texName = 0;

cgl_ctx = 0;

}


This is rendered into a CAOpenGLLayer subclass.


I tried adding some nasty code that keeps track of in-use texture names, and if glGenTextures gives me an ID that's already in-use than I generate another one until it returns one that's not being used, and it does does not reliably work around whatever is going on.


So, does anyone have any idea what is going on, or have any tips on where to look? I've never seen anything like this before, and I'm stumped...


Edit: Apologies for the horrible formatting... it won't save my formatted text.

OS X glGenTextures texture name reuse/collision
 
 
Q