[cairo] cairo_font_extents behaviour

Vladimir Sadovnikov sadko4u at gmail.com
Wed Jun 30 19:47:13 UTC 2021


Hello all!

I'm trying to work with custom fonts (loaded by freetype library on Linux) 
within cairo library and noticed interesting thing.

When I perform cairo_font_extents, the number of references to the font becomes 
increased and the font becomes never freed after usage.

What I mean.

I perform the following code snippet:

> cairo_font_extents_t fe;
> cairo_set_font_face(cr, some_font);
> cairo_font_extents(cr, &fe);
> cairo_set_font_face(cr, NULL);

Before calling first set_font_face, the cairo_font_face_get_reference_count 
returns the value of 1, which is obviously correct since the caller code is 
owner of the custom font.

After calling the first set_font_face, the cairo_font_face_get_reference_count 
returns the value of 1, which is correct, too.

But after calling cairo_font_extents and before calling second 
cairo_set_font_face, the cairo_font_face_get_reference_count returns 4!

After calling last cairo_set_font_face, I get the 
cairo_font_face_get_reference_count returning the value of 3.

This is surprising behaviour for me (shouldn't the cairo_font_extents release 
all acquired references before returning?)

And this yields to the memory leakage when I actively load/unload fonts since 
cairo doesn't ever release these fonts because of the reference counter has been 
increased.

Could you please explain what's happening and how to properly unload fonts so 
cairo library could finally release them without any memory leakage?


Best,

Vladimir





More information about the cairo mailing list