When I checked my blog’s Google Analytics stats, I noticed that someone looked for a way to iterate over a GList and remove its elements at the same time.
This is quite a common problem assuming that you deal with some objects appended to the list. The object can have some expiration timeout or can be marked for lazy removal. An independent process can loop through the list from time to time and remove those marked for releasing. This simple code should do the thing:

/* Set the iterator to the beginning of our list */
GList *node_itr = priv->_list;
while (node_itr != NULL)
{
   GObject *obj = (GObject *)node_itr->data;
   /* Store next element's pointer before removing it */
   GList *next = g_list_next(node_itr);
   /* Some dummy removal condition */
   if (my_gobject_marked_for_removal(obj))
   {
      g_object_unref(obj);
      priv->_list = g_list_delete_link(priv->_list, node_itr);
   }
   node_itr = next;
}
Category:
Blog Entry
Tags:
, ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>