From cdea5ca7958ff2c458fda177369e661b01bc697a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 23 May 2019 11:27:33 +0200 Subject: [PATCH] dispatcher: keep the GDBusConnection instance in the global variable It's anyway a singleton that is still referenced by other components. So unrefing it in the mainloop does not actually release any memory. However, the GDBusConnection singleton is fundamental for the run of the program. Keep it accessible in the global variables. Note that soon I will drop the GDBusInterfaceSkeleton and only operate on the GDBusConnection. Then it makes more sense to keep it around. Note that usually we want to keep the amount of global state small. But this connection is anyway a singleton (that we already implicitly use). So, it doesn't change the amount of global state nor does it really have much state (we either have a reference to the singleton or we don't). --- dispatcher/nm-dispatcher.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/dispatcher/nm-dispatcher.c b/dispatcher/nm-dispatcher.c index 8f1d45a1ba..febbfd4192 100644 --- a/dispatcher/nm-dispatcher.c +++ b/dispatcher/nm-dispatcher.c @@ -37,6 +37,7 @@ #include "nmdbus-dispatcher.h" static struct { + GDBusConnection *dbus_connection; GMainLoop *loop; gboolean debug; gboolean persist; @@ -988,7 +989,6 @@ int main (int argc, char **argv) { gs_free_error GError *error = NULL; - GDBusConnection *bus; Handler *handler = NULL; guint signal_id_term = 0; guint signal_id_int = 0; @@ -1015,8 +1015,8 @@ main (int argc, char **argv) gl.loop = g_main_loop_new (NULL, FALSE); - bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); - if (!bus) { + gl.dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (!gl.dbus_connection) { _LOG_X_W ("Could not get the system bus (%s). Make sure the message bus daemon is running!", error->message); gl.exit_with_failure = TRUE; @@ -1025,7 +1025,7 @@ main (int argc, char **argv) handler = g_object_new (HANDLER_TYPE, NULL); g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (handler->dbus_dispatcher), - bus, + gl.dbus_connection, NM_DISPATCHER_DBUS_PATH, &error); if (error) { @@ -1034,13 +1034,12 @@ main (int argc, char **argv) goto done; } - g_bus_own_name_on_connection (bus, + g_bus_own_name_on_connection (gl.dbus_connection, NM_DISPATCHER_DBUS_SERVICE, G_BUS_NAME_OWNER_FLAGS_NONE, on_name_acquired, on_name_lost, NULL, NULL); - g_object_unref (bus); quit_timeout_reschedule (); @@ -1061,10 +1060,10 @@ done: nm_clear_g_source (&signal_id_int); nm_clear_g_source (&gl.quit_id); g_clear_pointer (&gl.loop, g_main_loop_unref); + g_clear_object (&gl.dbus_connection); if (!gl.debug) logging_shutdown (); return gl.exit_with_failure ? 1 : 0; } -