[cairo] Memory leak in text functions?

Carl Worth cworth at cworth.org
Mon May 22 10:01:19 PDT 2006


On Mon, 22 May 2006 17:07:16 +0200 (CEST), Jan Slupski wrote:
> I'm using Cairo to create image surface, and when trying to 
> use any text related function (eg. cairo_text_extents),
> I'm observing quite big memory leak.

This is not actually a leak. First, note that valgrind indicates this
memory is "still reachable" and not "definitely lost".

What you are seeing here is font caches that are implemented as static
data within cairo, since they are designed to be shared across
multiple cairo_t objects.

So, cairo_destroy() is not sufficient for cleaning these things up.

Now, I happen to be the kind of person that likes to be able to run
valgrind and get a totally clean report, without any "still reachable"
memory. It turns out it's a fair amount of work to make this
possible. Every library needs a function that cleans up any statically
allocated data like this.

For its part, cairo has the function:

	cairo_debug_reset_static_data

for this very purpose.

In the 1.0 source code, the prototype for this function was in
cairo-debug.h which was never actually installed. I think that making
a separate header file for this function was a mistake, so I just took
advantage of the fact that we never installed it, and I've removed
that file in favor of just shoving the prototype into cairo.h.

So if you update your cairo source to the latest in git, then you
should be able to just add a call like follows to your program just
before it returns:

	cairo_debug_reset_static_data ();

and this "leak" should all go away [*].

Note that this function is only useful for testing that cairo is
properly cleaning up after itself, (and getting any noise from cairo's
"still reachable" memory off your report so that you can focus on any
of your own "still reachable" cleanup).

It's a very bad idea to call this function in any code that is meant
to be usable. It's actively discarding font caches, so text
performance would be expected to suffer considerably.

-Carl

[*] Or at least, everything that is cairo's responsibility will. The
bulk of the "still reachable" memory belongs to fontconfig. I wrote a
similar cleanup function for fontconfig a while back called FcFini. So
you would call that as well to clean everything up. (Hmm... from the
latest fontconfig it looks like there's a double-free error in the
FcFini path somewhere.)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20060522/74e2baa4/attachment.pgp


More information about the cairo mailing list