From 3baf56b4741435b6092ec2b2d9a1c2704f685b95 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 17 Nov 2018 12:56:14 +0100 Subject: [PATCH] keep-alive: cleanup tracking visible state of connection - in nm_keep_alive_set_settings_connection_watch_visible(), abort early when setting the same connection again (or %NULL again). - nm_keep_alive_set_settings_connection_watch_visible() would (already before) correctly disconnect the signal handler from the previous connection. As we anyway do explict disconnects, avoid the overhead of a weak pointer with g_signal_connect_object(). Just reuse the same setter to disconnect the signal in dispose(). - fix leaking priv->connection in nm_keep_alive_set_settings_connection_watch_visible(). - use g_signal_handlers_disconnect_by_func(), to be more specific about which signal we want to disconnect. --- src/nm-keep-alive.c | 47 ++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/nm-keep-alive.c b/src/nm-keep-alive.c index 4d67d907be..cfad13e6a3 100644 --- a/src/nm-keep-alive.c +++ b/src/nm-keep-alive.c @@ -135,22 +135,41 @@ connection_flags_changed (NMSettingsConnection *connection, _notify_alive (self); } +static void +_set_settings_connection_watch_visible (NMKeepAlive *self, + NMSettingsConnection *connection, + gboolean emit_signal) +{ + NMKeepAlivePrivate *priv = NM_KEEP_ALIVE_GET_PRIVATE (self); + gs_unref_object NMSettingsConnection *old_connection = NULL; + + if (priv->connection == connection) + return; + + if (priv->connection) { + g_signal_handlers_disconnect_by_func (priv->connection, + G_CALLBACK (connection_flags_changed), + self); + old_connection = g_steal_pointer (&priv->connection); + } + + if (connection) { + priv->connection = g_object_ref (connection); + g_signal_connect (priv->connection, + NM_SETTINGS_CONNECTION_FLAGS_CHANGED, + G_CALLBACK (connection_flags_changed), + self); + } + + if (emit_signal) + _notify_alive (self); +} + void nm_keep_alive_set_settings_connection_watch_visible (NMKeepAlive *self, NMSettingsConnection *connection) { - NMKeepAlivePrivate *priv = NM_KEEP_ALIVE_GET_PRIVATE (self); - - if (priv->connection) { - g_signal_handlers_disconnect_by_data (priv->connection, self); - priv->connection = NULL; - } - - priv->connection = g_object_ref (connection); - g_signal_connect_object (priv->connection, NM_SETTINGS_CONNECTION_FLAGS_CHANGED, - G_CALLBACK (connection_flags_changed), self, 0); - - _notify_alive (self); + _set_settings_connection_watch_visible (self, connection, TRUE); } /*****************************************************************************/ @@ -265,10 +284,8 @@ static void dispose (GObject *object) { NMKeepAlive *self = NM_KEEP_ALIVE (object); - NMKeepAlivePrivate *priv = NM_KEEP_ALIVE_GET_PRIVATE (self); - - g_clear_object (&priv->connection); + _set_settings_connection_watch_visible (self, NULL, FALSE); cleanup_dbus_watch (self); }