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

Brian Paul brianp at vmware.com
Wed Nov 23 07:49:40 PST 2011

On 11/23/2011 12:28 AM, zhigang gong wrote:
> 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
> counter.
> 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
> example:
> 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 2.
>                   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.

As long as the texture object is still bound to unit 0's 
GL_TEXTURE_2D, the texture will not get deleted.

When another texture gets bound to the binding point, the "deleted" 
texture's ref count should go to zero and really be deleted.  For 
example, binding the default texture object should do that:

glBindTexture(GL_TEXTURE_2D, 0);

If this isn't working, can you provide a test program?


More information about the mesa-dev mailing list