From 39ca3e51dfaa652ae4a0d936fff32a5edaca6c5f Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Mon, 14 Nov 2011 15:09:13 +0000 Subject: [PATCH] Don't take a reference to the last used onscreen framebuffer Cogl keeps a pointer to the last used onscreen framebuffer from the context to implement the deprecated cogl_set_draw_buffer function which can take COGL_WINDOW_BUFFER as the target to use the last onscreen buffer. Previously this would also take a reference to that pointer. However that was causing a circular reference between the framebuffer and the context which makes it impossible to clean up resources properly when onscreen buffers are used. This patch instead changes it to just store the pointer and then clear the pointer during _cogl_onscreen_free as a kind of cheap weak reference. Reviewed-by: Robert Bragg --- cogl/cogl-context.c | 6 ------ cogl/cogl-framebuffer.c | 18 ++++++++---------- cogl/cogl-onscreen.c | 3 +++ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/cogl/cogl-context.c b/cogl/cogl-context.c index da41a8204..b8296eceb 100644 --- a/cogl/cogl-context.c +++ b/cogl/cogl-context.c @@ -408,12 +408,6 @@ _cogl_context_free (CoglContext *context) winsys->context_deinit (context); - if (context->window_buffer) - { - cogl_object_unref (context->window_buffer); - context->window_buffer = NULL; - } - _cogl_free_framebuffer_stack (context->framebuffer_stack); if (context->current_path) diff --git a/cogl/cogl-framebuffer.c b/cogl/cogl-framebuffer.c index e28d44899..6d99e339b 100644 --- a/cogl/cogl-framebuffer.c +++ b/cogl/cogl-framebuffer.c @@ -1141,18 +1141,16 @@ notify_buffers_changed (CoglFramebuffer *old_draw_buffer, } } - /* XXX: - * To support the deprecated cogl_set_draw_buffer API we keep track - * of the last onscreen framebuffer that was set so that it can - * be restored if the COGL_WINDOW_BUFFER enum is used. */ + /* XXX: To support the deprecated cogl_set_draw_buffer API we keep + * track of the last onscreen framebuffer that was set so that it + * can be restored if the COGL_WINDOW_BUFFER enum is used. A + * reference isn't taken to the framebuffer because otherwise we + * would have a circular reference between the context and the + * framebuffer. Instead the pointer is set to NULL in + * _cogl_onscreen_free as a kind of a cheap weak reference */ if (new_draw_buffer && new_draw_buffer->type == COGL_FRAMEBUFFER_TYPE_ONSCREEN) - { - cogl_object_ref (new_draw_buffer); - if (ctx->window_buffer) - cogl_object_unref (ctx->window_buffer); - ctx->window_buffer = new_draw_buffer; - } + ctx->window_buffer = new_draw_buffer; } /* Set the current framebuffer without checking if it's already the diff --git a/cogl/cogl-onscreen.c b/cogl/cogl-onscreen.c index f95f96c71..f94399301 100644 --- a/cogl/cogl-onscreen.c +++ b/cogl/cogl-onscreen.c @@ -112,6 +112,9 @@ _cogl_onscreen_free (CoglOnscreen *onscreen) CoglFramebuffer *framebuffer = COGL_FRAMEBUFFER (onscreen); const CoglWinsysVtable *winsys = _cogl_framebuffer_get_winsys (framebuffer); + if (framebuffer->context->window_buffer == onscreen) + framebuffer->context->window_buffer = NULL; + winsys->onscreen_deinit (onscreen); _COGL_RETURN_IF_FAIL (onscreen->winsys == NULL);