Skip to content

Commit

Permalink
win32: Fix lifetime tracking of create_similar_image()
Browse files Browse the repository at this point in the history
As we return the child image to the user and so perform the reference
tracking on it and not the parent win32 display surface, we need to add
a call to destroy the parent from the image surface. This of course
complicates the normal scenario of destroying the parent first, and so
in that case we need to unhook the image->parent before freeing the
surface->image.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
  • Loading branch information
Chris Wilson committed Feb 15, 2012
1 parent dfb8b13 commit df608e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/cairo-image-surface-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ struct _cairo_image_surface {

pixman_image_t *pixman_image;
const cairo_compositor_t *compositor;

/* Parenting is tricky wrt lifetime tracking...
*
* One use for tracking the parent of an image surface is for
* create_similar_image() where we wish to create a device specific
* surface but return an image surface to the user. In such a case,
* the image may be owned by the device specific surface, its parent,
* but the user lifetime tracking is then performed on the image. So
* when the image is then finalized we call cairo_surface_destroy()
* on the parent. However, for normal usage where the lifetime tracking
* is done on the parent surface, we need to be careful to unhook
* the image->parent pointer before finalizing the image.
*/
cairo_surface_t *parent;

pixman_format_code_t pixman_format;
Expand Down
5 changes: 5 additions & 0 deletions src/cairo-image-surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,11 @@ _cairo_image_surface_finish (void *abstract_surface)
surface->data = NULL;
}

if (surface->parent) {
cairo_surface_destroy (surface->parent);
surface->parent = NULL;
}

return CAIRO_STATUS_SUCCESS;
}

Expand Down
6 changes: 5 additions & 1 deletion src/win32/cairo-win32-display-surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,12 @@ _cairo_win32_display_surface_finish (void *abstract_surface)
{
cairo_win32_display_surface_t *surface = abstract_surface;

if (surface->image)
if (surface->image) {
/* Unhook ourselves first to avoid the double-unref from the image */
surface->image->parent = NULL;
cairo_surface_finish (surface->image);
cairo_surface_destroy (surface->image);
}

/* If we created the Bitmap and DC, destroy them */
if (surface->bitmap) {
Expand Down

0 comments on commit df608e0

Please sign in to comment.