[Mesa-dev] Question about the texture's reference count processing in _mesa_update_texture

zhigang gong zhigang.gong at gmail.com
Tue Nov 22 23:28:34 PST 2011

Hi guys,

I have a question about the internal implementation of glDrawArrays or any
rendering functions.
In the code path, it calls to _mesa_update_texture(), and if there is
really a complete texture,
it will call _mesa_reference_texobj to set it to _Current, and increase the
texture object's reference

My question is when it will decrease the reference counter? This implicit
increasing seems
causes the reference counter unbalanced, and then will cause unexpected
behaviour. Here is an

1       glGenTextures(1, &tex);
2       glActiveTexture(GL_TEXTURE0);
3       glBindTexture(GL_TEXTURE_2D, tex);
4       glTexImage2D(GL_TEXTURE_2D, 0, iformat,
                               w, h, 0, format, type, bits);
5       glEnable(GL_TEXTURE_2D);
6       glUseProgram(shader_prog);
7        ... /*setup attribute array here.*/
8       glDrawArray(GL_TRIANGLE_FAN, 0, 4);
9       glUseProgram(0);
10     glDeleteTexture(1, &tex);

At Line 1, tex object is created with reference count initialized to 1.
At Line 3, tex object's reference count increased to 2.
At Line 8, it implicit increases tex object's reference count to 3.
At Line 10, it implict unbinds the tex object which decreases the count to
                 then it try to unreference the tex object which decreases
the count to1.

You can see that finally, the tex object's reference's count is 1 not zero
and thus it will not be freed. This is not what I expected.

Is there any mistakes in my use case? Or anyone can tell me how to
make sure the texture object get freed after the usage of it.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20111123/01cca5bf/attachment.html>

More information about the mesa-dev mailing list