[cairo] Fwd: _get_image_surface() function has memory leak

Carl Worth cworth at cworth.org
Mon Jun 22 13:37:08 PDT 2009

On Mon, 2009-06-22 at 06:01 +0000, Deok Jin Kim wrote:
> Title : _get_image_surface() function has memory leak

No, not a leak.

>            ximage = XGetImage (…);
>            image = _cairo_image_surface_create_with_pixman_format
> ((unsigned char *) ximage->data, …)
>            _cairo_image_surface_assume_ownership_of_data (image);
>            ximage->data = NULL;
> In above code, ximage->data= NULL prevents libX11.so.6 from freeing
> ximage->data.

Yes, that's intentional. This function uses XDestroyImage to free the
wrapper for the XImage, but explicitly does not want to free the image
data at this point. Instead, the function is returning (via image_out) a
new cairo image surface which contains the data.

Eventually, up above in the call chain the image surface will no longer
be used, (for example, its lifetime could be as long as a software
fallback), at which point cairo_surface_destroy will be called, leading
to the following code in _cairo_image_surface_finish:

    if (surface->owns_data) {
        free (surface->data);
        surface->data = NULL;

And here, the call to _cairo_image_surface_assume_ownership_of_data is
what sets surface->owns_data to TRUE.

So there is no leak in _get_image_surface---it's returning exactly what
it should be. It's conceivable that there's somewhere up above the call
chain that's neglecting to call cairo_surface_destroy and hence leaking.
Do you have actual evidence of any leak?


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.cairographics.org/archives/cairo/attachments/20090622/be5f4d10/attachment.pgp 

More information about the cairo mailing list