[cairo] cairo_font_face_t not freeing

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Tue May 2 20:31:55 UTC 2017


On Tue, 2 May 2017 14:18:10 -0400, Louis M wrote:

> I understand that. But this is not my question. What I want to know is
> why, after I free the context, the surface and the font_face, there is
> still have a reference to the font_face.

Hmmm ... it seems to have something to do with the
get_scaled_font call. When I stick extra prints of the font_face
refcount around there, it goes up by another 1 after the call.

That is, after font_face_create, the refcount is 1; after set_font_face
into the context, it is 2; then after get_scaled_font, it is 3. Then
after destroying the context, it is back to 2 again.

The doc
<https://www.cairographics.org/manual/cairo-text.html#cairo-set-scaled-font>
says the returned reference is owned by Cairo. But if I duplicate the
get_scaled_font call, it does not add another 1 to the refcount. So it
looks like the reference is only being created on the first call.

Interestingly, all this still happens if you comment out the
set_font_face call--that is, the get_scaled_font call still manages to
increment the refcount of the font_face by 1, even though you never set
it as the font face in the context! In this situation, the refcount
goes up to 2 after the set_font_size call.

However, this is only if you leave the font name as the empty string
in the toy_font_face_create call. If you specify some other font (e.g.
“dejavu serif” is a common one on Linux systems), then don’t call
set_font_face, then the refcount stays at 1 all the way up to the
dispose.

But again, once you set_font_face, then get_scaled_font, the refcount
gets as high as 3 as before.

In other words, the toy_font_face_create call with an empty name is
returning another reference to the default font that Cairo would use if
you don’t specify a font.

My guess is, that extra reference is coming from Cairo’s internal
font-caching machinery. So presumably Cairo knows to clean it up
properly when you call font_face_destroy...


More information about the cairo mailing list