[cairo] Reference counting of cairo free type face

Chris Wilson chris at chris-wilson.co.uk
Wed Nov 14 01:01:47 PST 2012

On Tue, 13 Nov 2012 22:17:00 +0100, Julian Viereck <julian.viereck at googlemail.com> wrote:
> Hi there,
> I try to add font support to the node-canvas [1] library, which uses 
> cairo for rendering. I use the freetype2 library to load a font face and 
> then use it in cairo. The font rendering is working, but I got stuck on 
> memory management of the cairo font face. In particular, the cairo font 
> face's reference counter is not reset to 1 at the point I expect it to be.
> Here's what I'm doing. I concentrate on the essential parts only. Let 
> "ref(cr_face)" be "cairo_font_face_get_reference_count(cr_face)" to get 
> the internal reference counter for the cairo font face.
>      // Create a free type font face from a font file.
>      FT_New_Face(library, *filePath, faceIdx, &ft_face);
>      // Create a cairo font face.
>      cr_face = cairo_ft_font_face_create_for_ft_face(ft_face, 0);
>      // At this point ref(cr_face) == 1.
>      // Set the cairo font face on a cairo rendering context.
>      cairo_set_font_face(ctx, cr_face);
>      // At this point ref(cr_face) == 2. I guess that's okay, as the 
> font is requird for rendering.
>      cairo_text_path(ctx, str);
>      // At this point ref(cr_face) == 4.
>      cairo_destroy(ctx);
>      // At this point ref(cr_face) == 3.
> Shouldn't the reference count after the "cairo_destory" be 1 again and 
> the cairo font face deleted? Do I need to perform some manually cleanup 
> first?

Cairo maintains a holdover cache of the MRU scaled fonts (outside of any
context) which accounts for the extra references. The
cairo_font_face_finish() / cairo_scaled_font_finish() interface has been
proposed to decouple those extra references in a user-controllable way.
However, in order to hook into the destroy notification for when the
font is released you want to use cairo_scaled_font_set_user_data().

Chris Wilson, Intel Open Source Technology Centre

More information about the cairo mailing list