From 040d28796a1344608cfd96000688effc219ce1fa Mon Sep 17 00:00:00 2001 From: Pierre-Luc Beaudoin Date: Sun, 31 May 2009 17:01:44 -0400 Subject: [PATCH] Fix Bug 584368 for ChamplainCache --- champlain/champlain-cache.c | 42 ++++++++++++++++++------ champlain/champlain-cache.h | 2 +- champlain/champlain-network-map-source.c | 6 ++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/champlain/champlain-cache.c b/champlain/champlain-cache.c index 4a6a34c..c342526 100644 --- a/champlain/champlain-cache.c +++ b/champlain/champlain-cache.c @@ -57,6 +57,8 @@ enum typedef struct _ChamplainCachePrivate ChamplainCachePrivate; +static ChamplainCache *instance = NULL; + struct _ChamplainCachePrivate { guint size_limit; @@ -142,6 +144,29 @@ champlain_cache_finalize (GObject *object) G_OBJECT_CLASS (champlain_cache_parent_class)->finalize (object); } +static GObject * +champlain_cache_constructor (GType type, + guint n_construct_params, + GObjectConstructParam *construct_params) +{ + GObject *retval; + + if (instance == NULL) + { + retval = G_OBJECT_CLASS (champlain_cache_parent_class)->constructor + (type, n_construct_params, construct_params); + + instance = CHAMPLAIN_CACHE (retval); + g_object_add_weak_pointer (retval, (gpointer) &instance); + } + else + { + retval = g_object_ref (instance); + } + + return retval; +} + static void champlain_cache_class_init (ChamplainCacheClass *klass) { @@ -149,6 +174,7 @@ champlain_cache_class_init (ChamplainCacheClass *klass) g_type_class_add_private (klass, sizeof (ChamplainCachePrivate)); + object_class->constructor = champlain_cache_constructor; object_class->get_property = champlain_cache_get_property; object_class->set_property = champlain_cache_set_property; object_class->dispose = champlain_cache_dispose; @@ -259,17 +285,9 @@ cleanup: * Since: 0.4 */ ChamplainCache* -champlain_cache_get_default (void) +champlain_cache_dup_default (void) { - static ChamplainCache * instance = NULL; - - if (G_UNLIKELY (instance == NULL)) - { - instance = g_object_new (CHAMPLAIN_TYPE_CACHE, NULL); - return instance; - } - - return g_object_ref (instance); + return g_object_new (CHAMPLAIN_TYPE_CACHE, NULL); } /** @@ -404,7 +422,10 @@ champlain_cache_fill_tile (ChamplainCache *self, g_strdup (filename)); if (priv->popularity_id == 0) + { + g_object_ref (self); priv->popularity_id = g_idle_add (inc_popularity, self); + } cleanup: sqlite3_reset (priv->stmt_select); @@ -452,6 +473,7 @@ inc_popularity (gpointer data) if (priv->popularity_queue == NULL) { + g_object_unref (cache); priv->popularity_id = 0; return FALSE; } diff --git a/champlain/champlain-cache.h b/champlain/champlain-cache.h index fe0d01e..818aa76 100644 --- a/champlain/champlain-cache.h +++ b/champlain/champlain-cache.h @@ -54,7 +54,7 @@ typedef struct { GType champlain_cache_get_type (void); -ChamplainCache* champlain_cache_get_default (void); +ChamplainCache* champlain_cache_dup_default (void); void champlain_cache_update_tile (ChamplainCache *self, ChamplainTile *tile, diff --git a/champlain/champlain-network-map-source.c b/champlain/champlain-network-map-source.c index 4e2bb40..d4d7aa2 100644 --- a/champlain/champlain-network-map-source.c +++ b/champlain/champlain-network-map-source.c @@ -421,7 +421,7 @@ file_loaded_cb (SoupSession *session, GFile *file; GFileInfo *info; guint filesize = 0; - ChamplainCache *cache = champlain_cache_get_default (); + ChamplainCache *cache = champlain_cache_dup_default (); filename = champlain_tile_get_filename (tile); @@ -545,6 +545,7 @@ file_loaded_cb (SoupSession *session, cleanup: g_object_unref (loader); + g_object_unref (cache); g_free (path); finish: champlain_tile_set_state (tile, CHAMPLAIN_STATE_DONE); @@ -562,7 +563,7 @@ fill_tile (ChamplainMapSource *map_source, ChamplainNetworkMapSource *source = CHAMPLAIN_NETWORK_MAP_SOURCE (map_source); ChamplainNetworkMapSourcePrivate *priv = source->priv; - ChamplainCache *cache = champlain_cache_get_default (); + ChamplainCache *cache = champlain_cache_dup_default (); /* Try the cached version first */ filename = get_filename (source, tile); @@ -647,5 +648,6 @@ fill_tile (ChamplainMapSource *map_source, /* If a tile is neither in cache or can be fetched, do nothing, it'll show up * as empty */ + g_object_unref (cache); } -- 2.39.5