CamelSession: Have add_service() return a new reference.
authorMatthew Barnes <mbarnes@redhat.com>
Sun, 12 Aug 2012 12:23:15 +0000 (08:23 -0400)
committerMatthew Barnes <mbarnes@redhat.com>
Sun, 12 Aug 2012 12:44:39 +0000 (08:44 -0400)
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

index 09035cf..9a75b1d 100644 (file)
@@ -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