From cc4c57888715a1e8910ccca10327bf97382f3254 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Tue, 10 Apr 2012 19:21:55 +0100 Subject: [PATCH] fix cogl_context_new crash if fail to connect renderer If a NULL display is passed to cogl_context_new() then it has to implicitly create a CoglRenderer and CoglDisplay and propagate any resulting errors back to the user. Previously the implementation relied on passing a NULL renderer to cogl_display_new() as the means for implicitly connecting to a renderer. The problem with this though is that cogl_display_new() isn't designed to ever return NULL but if it failed to connect to a renderer automatically it would do and then cogl_context_new would pass NULL to cogl_display_setup() leading to a crash. This patch changes the implementation of cogl_context_new() to now explicitly create a CoglRenderer and connect to it if a NULL display is given. This way we can easily propagate any errors. In addition cogl_display_new has been changed to abort if it fails to implicitly connect to a renderer due to a NULL renderer argument. An application needing to gracefully handle problems connecting to a renderer at runtime should manually instantiate and connect a renderer passing a GError argument to cogl_renderer_connect. Reviewed-by: Neil Roberts --- cogl/cogl-context.c | 11 ++++++++++- cogl/cogl-display.c | 8 +------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cogl/cogl-context.c b/cogl/cogl-context.c index c15e2db17..94ce2f14c 100644 --- a/cogl/cogl-context.c +++ b/cogl/cogl-context.c @@ -188,7 +188,16 @@ cogl_context_new (CoglDisplay *display, memset (context->winsys_features, 0, sizeof (context->winsys_features)); if (!display) - display = cogl_display_new (NULL, NULL); + { + CoglRenderer *renderer = cogl_renderer_new (); + if (!cogl_renderer_connect (renderer, error)) + { + g_free (context); + return NULL; + } + + display = cogl_display_new (renderer, NULL); + } else cogl_object_ref (display); diff --git a/cogl/cogl-display.c b/cogl/cogl-display.c index 4818bda6f..9ac1b6888 100644 --- a/cogl/cogl-display.c +++ b/cogl/cogl-display.c @@ -91,13 +91,7 @@ cogl_display_new (CoglRenderer *renderer, display->renderer = cogl_renderer_new (); if (!cogl_renderer_connect (display->renderer, &error)) - { - g_warning ("Failed to connect renderer: %s\n", error->message); - g_error_free (error); - g_object_unref (display->renderer); - g_slice_free (CoglDisplay, display); - return NULL; - } + g_error ("Failed to connect to renderer: %s\n", error->message); display->onscreen_template = onscreen_template; if (onscreen_template)