From fb2fb46f2ba430c3c16d07ad5eff1a292f1597b0 Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Sun, 12 Aug 2012 08:23:15 -0400 Subject: [PATCH] CamelSession: Have add_service() return a new reference. Applying lessons learned from ESourceRegistry. When returning a pointer to a reference-counted object in a multi- threaded environment, always increase the object's reference count before returning so as to transfer a new reference to the caller. Otherwise it introduces a potential race where the reference-counted object may lose its last reference and be freed while the caller is still using the object. Even if the caller immediately increments the object's reference count, it's still a potential race. Transferring a new reference to the caller means the caller must unreference the object when finished with it so the object will be properly freed when it's no longer needed. Making subtle behavioral changes like this without renaming the API is usually considered bad, but since Evolution is the only consumer we can easily keep the side-effects under control. --- camel/camel-session.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/camel/camel-session.c b/camel/camel-session.c index 09035cf..9a75b1d 100644 --- a/camel/camel-session.c +++ b/camel/camel-session.c @@ -422,13 +422,13 @@ session_add_service (CamelSession *session, "provider", provider, "session", session, "uid", uid, NULL); - /* The hash table takes ownership of the new CamelService. */ if (service != NULL) { g_mutex_lock (session->priv->services_lock); g_hash_table_insert ( session->priv->services, - g_strdup (uid), service); + g_strdup (uid), + g_object_ref (service)); g_mutex_unlock (session->priv->services_lock); } @@ -943,6 +943,9 @@ camel_session_get_user_cache_dir (CamelSession *session) * if the #CamelProvider does not specify a valid #GType for @type, the * function sets @error and returns %NULL. * + * The returned #CamelService is referenced for thread-safety and must be + * unreferenced with g_object_unref() when finished with it. + * * Returns: a #CamelService instance, or %NULL * * Since: 3.2 -- 2.7.4