[grilo/wip/hadess/network-sources-crasher: 2/3] registry: Fix g_hash_table_iter_next() warning



commit 164a72a25bdbd594a924e2623f3940edf0141e3a
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Nov 16 13:24:13 2018 +0100

    registry: Fix g_hash_table_iter_next() warning
    
    As we loop through the sources as listed in the hash table, there's a
    good chance that the hash table will be modified as sources are added or
    removed. But as we shouldn't be modifying the hash table while it's
    being iterated over, the code throws a warning, and will exit the loop.
    
    Warning: g_hash_table_iter_next: assertion 'ri->version == ri->hash_table->version' failed
    
    Solve this by getting a list of all the sources in advance.

 src/grl-registry.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
---
diff --git a/src/grl-registry.c b/src/grl-registry.c
index 64ed193..2f1648d 100644
--- a/src/grl-registry.c
+++ b/src/grl-registry.c
@@ -220,17 +220,22 @@ network_changed_cb (GObject     *gobject,
 {
   GNetworkConnectivity connectivity;
   gboolean network_available;
-  GHashTableIter iter;
   GrlSource *current_source;
+  GList *sources, *l;
 
   GRL_DEBUG ("Network availability changed");
   get_connectivity (registry, &connectivity, &network_available);
 
+  sources = g_hash_table_get_values (registry->priv->sources);
+  if (!sources)
+    return;
+
   if (!network_available) {
-    g_hash_table_iter_init (&iter, registry->priv->sources);
+    for (l = sources; l != NULL; l = l->next) {
+      const char **tags;
 
-    while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &current_source)) {
-      const char **tags = grl_source_get_tags (current_source);
+      current_source = l->data;
+      tags = grl_source_get_tags (current_source);
 
       if (!tags)
         continue;
@@ -245,9 +250,11 @@ network_changed_cb (GObject     *gobject,
       }
     }
   } else {
-    g_hash_table_iter_init (&iter, registry->priv->sources);
-    while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &current_source)) {
-      const char **tags = grl_source_get_tags (current_source);
+    for (l = sources; l != NULL; l = l->next) {
+      const char **tags;
+
+      current_source = l->data;
+      tags = grl_source_get_tags (current_source);
 
       if (!tags)
         continue;
@@ -279,6 +286,8 @@ network_changed_cb (GObject     *gobject,
       }
     }
   }
+
+  g_list_free (sources);
 }
 
 static void


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