From 10fb9c744b01451f037466c941f018257a674878 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 31 Jan 2011 21:29:05 -0600 Subject: Make sure to clear the address when clients change. --- src/datetime-service.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/datetime-service.c b/src/datetime-service.c index 8a31940..04a808a 100644 --- a/src/datetime-service.c +++ b/src/datetime-service.c @@ -393,6 +393,8 @@ geo_create_address (GeoclueMasterClient * master, GeoclueAddress * address, GErr return; } + g_warn_if_fail(geo_address == NULL); + g_debug("Created Geoclue Address"); geo_address = address; g_object_ref(G_OBJECT(geo_address)); @@ -420,6 +422,13 @@ geo_client_invalid (GeoclueMasterClient * client, gpointer user_data) { g_warning("Master client invalid, rebuilding."); + /* Client changes we can assume the address is invalid */ + if (geo_address != NULL) { + g_object_unref(G_OBJECT(geo_address)); + } + geo_address = NULL; + + /* And our master client is invalid */ if (geo_master != NULL) { g_object_unref(G_OBJECT(geo_master)); } @@ -470,6 +479,12 @@ geo_create_client (GeoclueMaster * master, GeoclueMasterClient * client, gchar * geo_master = client; g_object_ref(G_OBJECT(geo_master)); + /* New client, make sure we don't have an address hanging on */ + if (geo_address != NULL) { + g_object_unref(G_OBJECT(geo_address)); + } + geo_address = NULL; + geoclue_master_client_set_requirements_async(geo_master, GEOCLUE_ACCURACY_LEVEL_REGION, 0, -- cgit v1.2.3 From 004f46321f23d5967f698dd1201bc19b8a422dbf Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Feb 2011 09:35:04 -0600 Subject: Adding more comments --- src/datetime-service.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/datetime-service.c b/src/datetime-service.c index 04a808a..db887a9 100644 --- a/src/datetime-service.c +++ b/src/datetime-service.c @@ -393,6 +393,9 @@ geo_create_address (GeoclueMasterClient * master, GeoclueAddress * address, GErr return; } + /* We shouldn't have created a new address if we already had one + so this is a warning. But, it really is only a mem-leak so we + don't need to error out. */ g_warn_if_fail(geo_address == NULL); g_debug("Created Geoclue Address"); @@ -422,7 +425,8 @@ geo_client_invalid (GeoclueMasterClient * client, gpointer user_data) { g_warning("Master client invalid, rebuilding."); - /* Client changes we can assume the address is invalid */ + /* Client changes we can assume the address is now invalid so we + need to unreference the one we had. */ if (geo_address != NULL) { g_object_unref(G_OBJECT(geo_address)); } @@ -453,6 +457,8 @@ geo_address_change (GeoclueMasterClient * client, gchar * a, gchar * b, gchar * { g_warning("Address provider changed. Let's change"); + /* If the address is supposed to have changed we need to drop the old + address before starting to get the new one. */ if (geo_address != NULL) { g_object_unref(G_OBJECT(geo_address)); } -- cgit v1.2.3 From 96ab5fe4d0dc4d8221e05116f6babcaf66cf2379 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Feb 2011 09:39:28 -0600 Subject: Put the geo_address clean up in a small helper function. --- src/datetime-service.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/datetime-service.c b/src/datetime-service.c index db887a9..2a9ccdb 100644 --- a/src/datetime-service.c +++ b/src/datetime-service.c @@ -384,6 +384,23 @@ geo_address_cb (GeoclueAddress * address, int timestamp, GHashTable * addy_data, return; } +/* Clean up the reference we kept to the address and make sure to + drop the signals incase someone else has one. */ +static void +geo_address_clean (void) +{ + if (geo_address == NULL) { + return; + } + + g_signal_handlers_disconnect_by_func(G_OBJECT(geo_address), geo_address_cb, NULL); + g_object_unref(G_OBJECT(geo_address)); + + geo_address = NULL; + + return; +} + /* Callback from creating the address */ static void geo_create_address (GeoclueMasterClient * master, GeoclueAddress * address, GError * error, gpointer user_data) @@ -397,6 +414,7 @@ geo_create_address (GeoclueMasterClient * master, GeoclueAddress * address, GErr so this is a warning. But, it really is only a mem-leak so we don't need to error out. */ g_warn_if_fail(geo_address == NULL); + geo_address_clean(); g_debug("Created Geoclue Address"); geo_address = address; @@ -427,10 +445,7 @@ geo_client_invalid (GeoclueMasterClient * client, gpointer user_data) /* Client changes we can assume the address is now invalid so we need to unreference the one we had. */ - if (geo_address != NULL) { - g_object_unref(G_OBJECT(geo_address)); - } - geo_address = NULL; + geo_address_clean(); /* And our master client is invalid */ if (geo_master != NULL) { @@ -459,10 +474,7 @@ geo_address_change (GeoclueMasterClient * client, gchar * a, gchar * b, gchar * /* If the address is supposed to have changed we need to drop the old address before starting to get the new one. */ - if (geo_address != NULL) { - g_object_unref(G_OBJECT(geo_address)); - } - geo_address = NULL; + geo_address_clean(); geoclue_master_client_create_address_async(geo_master, geo_create_address, NULL); @@ -486,10 +498,7 @@ geo_create_client (GeoclueMaster * master, GeoclueMasterClient * client, gchar * g_object_ref(G_OBJECT(geo_master)); /* New client, make sure we don't have an address hanging on */ - if (geo_address != NULL) { - g_object_unref(G_OBJECT(geo_address)); - } - geo_address = NULL; + geo_address_clean(); geoclue_master_client_set_requirements_async(geo_master, GEOCLUE_ACCURACY_LEVEL_REGION, -- cgit v1.2.3 From 5de989fb1e270fbc9381b432e49df6790f985258 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Feb 2011 09:48:29 -0600 Subject: Changing the clean up of the client to be in a function and drop the signals as well. --- src/datetime-service.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/datetime-service.c b/src/datetime-service.c index 2a9ccdb..59308dd 100644 --- a/src/datetime-service.c +++ b/src/datetime-service.c @@ -401,6 +401,24 @@ geo_address_clean (void) return; } +/* Clean up and remove all signal handlers from the client as we + unreference it as well. */ +static void +geo_client_clean (void) +{ + if (geo_master == NULL) { + return; + } + + g_signal_handlers_disconnect_by_func(G_OBJECT(geo_master), geo_client_invalid, NULL); + g_signal_handlers_disconnect_by_func(G_OBJECT(geo_master), geo_address_change, NULL); + g_object_unref(G_OBJECT(geo_master)); + + geo_master = NULL; + + return; +} + /* Callback from creating the address */ static void geo_create_address (GeoclueMasterClient * master, GeoclueAddress * address, GError * error, gpointer user_data) @@ -448,10 +466,7 @@ geo_client_invalid (GeoclueMasterClient * client, gpointer user_data) geo_address_clean(); /* And our master client is invalid */ - if (geo_master != NULL) { - g_object_unref(G_OBJECT(geo_master)); - } - geo_master = NULL; + geo_client_clean(); GeoclueMaster * master = geoclue_master_get_default(); geoclue_master_create_client_async(master, geo_create_client, NULL); -- cgit v1.2.3 From e5e4415f3629352b5bc294acfec9593228d068ee Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Feb 2011 09:49:45 -0600 Subject: Putting the clear functions in shutdown so the objects get cleaned up nicely there as well. --- src/datetime-service.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/datetime-service.c b/src/datetime-service.c index 59308dd..04e3d6b 100644 --- a/src/datetime-service.c +++ b/src/datetime-service.c @@ -582,6 +582,9 @@ main (int argc, char ** argv) mainloop = g_main_loop_new(NULL, FALSE); g_main_loop_run(mainloop); + geo_address_clean(); + geo_client_clean(); + g_object_unref(G_OBJECT(master)); g_object_unref(G_OBJECT(dbus)); g_object_unref(G_OBJECT(service)); -- cgit v1.2.3 From c67f50916bd8cfe718ceed73ee95912d898df3d1 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Tue, 1 Feb 2011 09:52:10 -0600 Subject: Forgot to bring up the prototypes. --- src/datetime-service.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/datetime-service.c b/src/datetime-service.c index 04e3d6b..9f795d1 100644 --- a/src/datetime-service.c +++ b/src/datetime-service.c @@ -40,6 +40,8 @@ with this program. If not, see . static void geo_create_client (GeoclueMaster * master, GeoclueMasterClient * client, gchar * path, GError * error, gpointer user_data); static void setup_timer (void); +static void geo_client_invalid (GeoclueMasterClient * client, gpointer user_data); +static void geo_address_change (GeoclueMasterClient * client, gchar * a, gchar * b, gchar * c, gchar * d, gpointer user_data); static IndicatorService * service = NULL; static GMainLoop * mainloop = NULL; -- cgit v1.2.3