[liboobs] Make OobsSession a real singleton



commit 71c3c9b4a14d81bb390641da459a63861425eeed
Author: Milan Bouchet-Valat <nalimilan club fr>
Date:   Mon Jan 4 19:18:42 2010 +0100

    Make OobsSession a real singleton
    
    Documentation comment said it was a singleton, but every new OobsObject called oobs_session_get(), which was simply running g_object_new(). Now we keep the reference to the object if it exists, and return it. Every OobsObject now adds a reference to it in init(), and releases it in finalize(), right after calling _oobs_session_unregister_object(). Else this function would crash when client programs unreference the session before the OobsObjects.

 oobs/oobs-object.c  |    6 ++++++
 oobs/oobs-session.c |    7 ++++++-
 2 files changed, 12 insertions(+), 1 deletions(-)
---
diff --git a/oobs/oobs-object.c b/oobs/oobs-object.c
index 391b993..9f3cc23 100644
--- a/oobs/oobs-object.c
+++ b/oobs/oobs-object.c
@@ -167,7 +167,10 @@ oobs_object_init (OobsObject *object)
   OobsObjectPrivate *priv;
 
   priv = OOBS_OBJECT_GET_PRIVATE (object);
+
+  /* We need session to stay alive until object is destroyed */
   priv->session = oobs_session_get ();
+  g_object_ref (priv->session);
   priv->remote_object = NULL;
   dbus_error_init (&priv->dbus_error);
 
@@ -192,6 +195,9 @@ oobs_object_finalize (GObject *object)
 
   _oobs_session_unregister_object (priv->session, obj);
 
+  /* Only now, we don't care about session */
+  g_object_unref (priv->session);
+
   g_free (priv->remote_object);
   g_free (priv->path);
   g_free (priv->method);
diff --git a/oobs/oobs-session.c b/oobs/oobs-session.c
index dd9d66a..b6bdb41 100644
--- a/oobs/oobs-session.c
+++ b/oobs/oobs-session.c
@@ -220,7 +220,12 @@ oobs_session_get_property (GObject      *object,
 OobsSession*
 oobs_session_get (void)
 {
-  return g_object_new (OOBS_TYPE_SESSION, NULL);
+  static OobsSession *session = NULL;
+
+  if (!session)
+    session = g_object_new (OOBS_TYPE_SESSION, NULL);
+
+  return session;
 }
 
 /**



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]