From 8cac8c42f15a4d07bd0aa8c95fc2ee729828d6d1 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Beaudoin Date: Sun, 31 May 2009 18:20:01 -0400 Subject: [PATCH] Fix Bug 584368 for ChamplainMapSourceFactory --- champlain/champlain-cache.c | 6 ++-- champlain/champlain-map-source-factory.c | 44 +++++++++++++++++------- champlain/champlain-map-source-factory.h | 2 +- champlain/champlain-view.c | 2 +- demos/launcher-gtk.c | 6 ++-- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/champlain/champlain-cache.c b/champlain/champlain-cache.c index c342526..77b5481 100644 --- a/champlain/champlain-cache.c +++ b/champlain/champlain-cache.c @@ -278,9 +278,11 @@ cleanup: } /** - * champlain_cache_get_default: + * champlain_cache_dup_default: + * + * Returns the #ChamplainCache singleton, use #g_object_unref when not neeeded + * anymore. * - * Returns the #ChamplainCache singleton * * Since: 0.4 */ diff --git a/champlain/champlain-map-source-factory.c b/champlain/champlain-map-source-factory.c index 2bd84a3..6c58383 100644 --- a/champlain/champlain-map-source-factory.c +++ b/champlain/champlain-map-source-factory.c @@ -62,6 +62,7 @@ enum }; /* static guint champlain_map_source_factory_signals[LAST_SIGNAL] = { 0, }; */ +static ChamplainMapSourceFactory *instance = NULL; G_DEFINE_TYPE (ChamplainMapSourceFactory, champlain_map_source_factory, G_TYPE_OBJECT); @@ -121,12 +122,37 @@ champlain_map_source_factory_finalize (GObject *object) G_OBJECT_CLASS (champlain_map_source_factory_parent_class)->finalize (object); } +static GObject * +champlain_map_source_factory_constructor (GType type, + guint n_construct_params, + GObjectConstructParam *construct_params) +{ + GObject *retval; + + if (instance == NULL) + { + retval = G_OBJECT_CLASS (champlain_map_source_factory_parent_class)->constructor + (type, n_construct_params, construct_params); + + instance = CHAMPLAIN_MAP_SOURCE_FACTORY (retval); + g_object_add_weak_pointer (retval, (gpointer) &instance); + } + else + { + retval = g_object_ref (instance); + } + + return retval; +} + static void champlain_map_source_factory_class_init (ChamplainMapSourceFactoryClass *klass) { g_type_class_add_private (klass, sizeof (ChamplainMapSourceFactoryPrivate)); GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->constructor = champlain_map_source_factory_constructor; object_class->finalize = champlain_map_source_factory_finalize; object_class->get_property = champlain_map_source_factory_get_property; object_class->set_property = champlain_map_source_factory_set_property; @@ -214,25 +240,17 @@ champlain_map_source_factory_init (ChamplainMapSourceFactory *factory) } /** - * champlain_map_source_factory_get_default: + * champlain_map_source_factory_dup_default: * - * Returns the singleton #ChamplainMapSourceFactory + * Returns the singleton #ChamplainMapSourceFactory, it should be freed + * using #g_object_unref when not needed. * * Since: 0.4 */ ChamplainMapSourceFactory * -champlain_map_source_factory_get_default (void) +champlain_map_source_factory_dup_default (void) { - static ChamplainMapSourceFactory *instance = NULL; - - if (G_UNLIKELY (instance == NULL)) - { - instance = g_object_new (CHAMPLAIN_TYPE_MAP_SOURCE_FACTORY, NULL); - g_object_add_weak_pointer (G_OBJECT (instance), (gpointer*)&instance); - return instance; - } - - return g_object_ref (instance); + return g_object_new (CHAMPLAIN_TYPE_MAP_SOURCE_FACTORY, NULL); } /** diff --git a/champlain/champlain-map-source-factory.h b/champlain/champlain-map-source-factory.h index 191d141..c2c441e 100644 --- a/champlain/champlain-map-source-factory.h +++ b/champlain/champlain-map-source-factory.h @@ -55,7 +55,7 @@ struct _ChamplainMapSourceFactoryClass GType champlain_map_source_factory_get_type (void); -ChamplainMapSourceFactory * champlain_map_source_factory_get_default (void); +ChamplainMapSourceFactory * champlain_map_source_factory_dup_default (void); GSList * champlain_map_source_factory_get_list (ChamplainMapSourceFactory *factory); diff --git a/champlain/champlain-view.c b/champlain/champlain-view.c index 474edbe..7bcf6fe 100644 --- a/champlain/champlain-view.c +++ b/champlain/champlain-view.c @@ -791,7 +791,7 @@ champlain_view_init (ChamplainView *view) view->priv = priv; - priv->factory = champlain_map_source_factory_get_default (); + priv->factory = champlain_map_source_factory_dup_default (); priv->map_source = champlain_map_source_factory_create (priv->factory, CHAMPLAIN_MAP_SOURCE_OSM_MAPNIK); priv->zoom_level = 0; priv->min_zoom_level = champlain_map_source_get_min_zoom_level (priv->map_source); diff --git a/demos/launcher-gtk.c b/demos/launcher-gtk.c index 3e05184..194d9fd 100644 --- a/demos/launcher-gtk.c +++ b/demos/launcher-gtk.c @@ -63,7 +63,7 @@ map_source_changed (GtkWidget *widget, gtk_tree_model_get (model, &iter, COL_ID, &id, -1); - ChamplainMapSourceFactory *factory = champlain_map_source_factory_get_default (); + ChamplainMapSourceFactory *factory = champlain_map_source_factory_dup_default (); source = champlain_map_source_factory_create (factory, id); if (source != NULL) @@ -141,9 +141,9 @@ build_combo_box (GtkComboBox *box) G_TYPE_STRING, /* name */ -1); - factory = champlain_map_source_factory_get_default (); + factory = champlain_map_source_factory_dup_default (); sources = champlain_map_source_factory_get_list (factory); - + iter = sources; while (iter != NULL) { -- 2.39.5