fixed function to iterate through hash table and remove hash entries
authorHolger Schemel <info@artsoft.org>
Tue, 27 Feb 2024 19:21:06 +0000 (20:21 +0100)
committerHolger Schemel <info@artsoft.org>
Tue, 27 Feb 2024 19:21:06 +0000 (20:21 +0100)
src/libgame/hash.c

index 2b856f6810df38cc739c62e7b2695b5586b1cc69..4dd904c9caac425fd3b485357980d3d67ffba6e1 100644 (file)
@@ -529,17 +529,21 @@ hashtable_foreach_remove(struct hashtable *h, hashtable_remove_fn fn, void *user
 
   free(itr);
 
-  struct hashtable_itr *itr_remove = hashtable_iterator(remove);
   unsigned int num_removed = 0;
 
-  do
+  if (hashtable_count(remove) > 0)
   {
-    hashtable_remove(h, hashtable_iterator_key(itr_remove));
-    num_removed++;
-  }
-  while (hashtable_iterator_advance(itr_remove));
+    struct hashtable_itr *itr_remove = hashtable_iterator(remove);
 
-  free(itr_remove);
+    do
+    {
+      hashtable_remove(h, hashtable_iterator_key(itr_remove));
+      num_removed++;
+    }
+    while (hashtable_iterator_advance(itr_remove));
+
+    free(itr_remove);
+  }
 
   hashtable_destroy(remove);