GNOME Bugzilla – Bug 723316
g_hash_table_iter_remove() should be explicit whether or not it is safe while iterating the table
Last modified: 2014-02-21 20:40:22 UTC
One of the most common crasher I've seen is removing elements of a data structure while iterating it. Typically: for (l = list; l != NULL; l = l->next) { if (cond) list = g_list_delete_link (list, l); } or: g_hash_table_foreach (table, {if (cond) g_hash_table_remove (table, key);}, NULL); or: while (g_hash_table_iter_next (&iter, &k, &v)) { if (cond) g_hash_table_remove (table, k); } Since it is really common mistake I think the doc of g_hash_table_iter_remove() should be explicit about that case. I _think_ it is safe to this, but I'm not entirely sure: while (g_hash_table_iter_next (&iter, &k, &v)) { if (cond) g_hash_table_iter_remove (&iter); }
While I'm at it, I'm also unsure about: for (iter = g_sequence_get_begin_iter (seq); !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter)) { if (cond) g_sequence_remove (iter); } The whole GSequence API is weird IMHO, but that's another topic :P
I don't know about GSequence, but you're right about the hashtable, calling g_hash_table_iter_remove() like so is safe.
Created attachment 269950 [details] [review] GHashTable: Explicitly document that _iter_remove() is safe while iterating
Company approved on IRC. Pushed into master.