title: DBus Glib with C++: Can't create a DBusGProxy, release it and create that again
body: <p>I'm working on a lib that uses Glib internally to connect to other modules.</p>
  
  <p>This lib has a initialize method that sets up the dbus connection and a terminate method that finalizes all the internal resources (including the dbus connection).
  At least it should do this.</p>
  
  <p>However, I can't make dbus calls again after the terminate method gets called (of course I called the initialize method again before calling the dbus methods again).</p>
  
  <p>I'm using C++ mainly (a few used libs are in C).</p>
  
  <p>Here is my internal dbus initializer:</p>
  
  <pre><code>#ifdef __cplusplus
  extern "C" {
  #endif
  
  #include &lt;dbus/dbus-glib.h&gt;
  #include &lt;stdio.h&gt;
  
  #define _DBUS_SERVICE       "Removed due to restrictions"
  #define _DBUS_PATH      "/"
  #define _DBUS_INTERFACE "Removed due to restrictions"
  
  static DBusGConnection * my_dbus_conn = NULL;
  static DBusGProxy * my_proxy = NULL;
  
  gboolean dbus_init() {
      GError *error = NULL;
      g_type_init();
      my_dbus_conn = dbus_g_bus_get(DBUS_BUS_SESSION, &amp;error);
      if (my_dbus_conn == NULL) {
          g_printerr("DBUS Connection Error (%s)\n", error-&gt;message);
          g_error_free(error);
          return false;
      }
      my_proxy = dbus_g_proxy_new_for_name(my_dbus_conn, _DBUS_SERVICE, _DBUS_PATH, _DBUS_INTERFACE);
      if (my_proxy == NULL) {
          g_printerr("ERROR: DBUS Proxy creation Error (%s)\n", error-&gt;message);
          g_error_free(error);
          return false;
      }
      return true;
  }
  
  DBusGProxy * dbus_get_proxy() {
      if (my_proxy == NULL) {
          dbus_init();
      }
      return my_proxy;
  }
  
  void dbus_term() {
      g_object_unref(G_OBJECT (my_proxy));
      dbus_g_connection_unref(my_dbus_conn);
      my_dbus_conn = NULL;
  }
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>Here is my calling code:</p>
  
  <pre><code>MY_G_DEBUG("Manager", "Creating Server IPC stub...");
  this-&gt;m_pDbusServer = get_proxy();
  
  if (this-&gt;m_pDbusServer == NULL) {
      return;
  }
  
  MY_G_DEBUG("Manager", "CheckPoint 1!");
  gboolean success;
  GError *gerror = NULL;
  char **l_pHelloWorldReply;
  
  success = dbus_server_helloworld(this-&gt;m_pDbusServer, "Hello World from Manager", &amp;l_pHelloWorldReply, &amp;gerror);
  
  MY_G_DEBUG("Manager", "CheckPoint 2!");
  
  if (success == FALSE) {
      MY_G_DEBUG("Manager", "CheckPoint 3!");
      if (gerror == NULL) {
          MY_G_DEBUG("Manager", "CRITICAL: Hello World call Failed (error not set)");
      } else {
          MY_G_DEBUG("Manager", "CRITICAL: Hello World call Failed (%s)", gerror-&gt;message);
          g_error_free(gerror);
          gerror = NULL;
      }
      return;
  } else {
      MY_G_DEBUG("Manager", "CheckPoint 4!");
      for (guint ii = 0; l_pHelloWorldReply[ii] != NULL; ii++) {
          MY_G_DEBUG("Manager", "Hello World response %d: %s", ii, l_pHelloWorldReply[ii]);
      }
  }
  
  MY_G_DEBUG("Manager", "CheckPoint 5!");
  </code></pre>
  
  <p>Here is the logging output:</p>
  
  <pre><code>(process:24498): Manager-DEBUG: Checkpoint: 1!
  (process:24498): Manager-DEBUG: Checkpoint: 2!
  (process:24498): Manager-DEBUG: Checkpoint: 3!
  (process:24498): Manager-DEBUG: CRITICAL: Hello World call Failed (error not set)
  </code></pre>
  
  <p>I got an error message everytime I call the dbus_term function, however I couldn't manage to fix it.</p>
  
  <pre><code>(process:24498): GLib-GObject-WARNING **: invalid uninstantiatable type `&lt;invalid&gt;' in cast to `GObject'
  (process:24498): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed
  </code></pre>
  
  <p>The first time I try to call methods, everything works well...
  I strongly believe that's caused by the dbus_term issue.</p>
  
  <p>Does anyone have any idea why that is happening?</p>
  
  <p>Thanks very much.</p>