diff options
author | Thomas Haller <thaller@redhat.com> | 2013-10-18 16:07:26 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2013-10-22 19:53:57 +0200 |
commit | 3eb1d5e9024108fe656476dc5a11fefd194d0c54 (patch) | |
tree | e59c0a5cef44de57babcf79784320092691a467e /callouts | |
parent | 1c93b24829d61e58624bf2dd20ebb34b3fceabea (diff) |
core: cleanup freeing of glib collections of pointers
When freeing one of the collections such as GArray, GPtrArray, GSList,
etc. it is common that the items inside the connections must be
freed/unrefed too.
The previous code often iterated over the collection first with
e.g. g_ptr_array_foreach and passing e.g. g_free as GFunc argument.
For one, this has the problem, that g_free has a different signature
GDestroyNotify then the expected GFunc. Moreover, this can be
simplified either by setting a clear function
(g_ptr_array_set_clear_func) or by passing the destroy function to the
free function (g_slist_free_full).
Signed-off-by: Thomas Haller <thaller@redhat.com>
Diffstat (limited to 'callouts')
-rw-r--r-- | callouts/nm-dispatcher-action.c | 10 | ||||
-rw-r--r-- | callouts/nm-dispatcher-utils.c | 24 |
2 files changed, 13 insertions, 21 deletions
diff --git a/callouts/nm-dispatcher-action.c b/callouts/nm-dispatcher-action.c index d37a08e1b2..397f2bac3e 100644 --- a/callouts/nm-dispatcher-action.c +++ b/callouts/nm-dispatcher-action.c @@ -121,8 +121,10 @@ struct Request { }; static void -script_info_free (ScriptInfo *info) +script_info_free (gpointer ptr) { + ScriptInfo *info = ptr; + g_free (info->script); g_free (info->error); g_free (info); @@ -134,10 +136,8 @@ request_free (Request *request) g_free (request->action); g_free (request->iface); g_strfreev (request->envp); - if (request->scripts) { - g_ptr_array_foreach (request->scripts, (GFunc) script_info_free, NULL); + if (request->scripts) g_ptr_array_free (request->scripts, TRUE); - } } static gboolean @@ -464,7 +464,7 @@ impl_dispatch (Handler *h, request->iface = g_strdup (iface); - request->scripts = g_ptr_array_sized_new (5); + request->scripts = g_ptr_array_new_full (5, script_info_free); for (iter = sorted_scripts; iter; iter = g_slist_next (iter)) { ScriptInfo *s = g_malloc0 (sizeof (*s)); s->request = request; diff --git a/callouts/nm-dispatcher-utils.c b/callouts/nm-dispatcher-utils.c index 4c047da0ed..ee756b648d 100644 --- a/callouts/nm-dispatcher-utils.c +++ b/callouts/nm-dispatcher-utils.c @@ -130,10 +130,8 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) } if (num) items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ADDRESSES=%d", prefix, num)); - if (addresses) { - g_slist_foreach (addresses, (GFunc) nm_ip4_address_unref, NULL); - g_slist_free (addresses); - } + if (addresses) + g_slist_free_full (addresses, (GDestroyNotify) nm_ip4_address_unref); /* DNS servers */ val = g_hash_table_lookup (ip4_config, "nameservers"); @@ -219,10 +217,8 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) items = g_slist_prepend (items, routetmp); } items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, num)); - if (routes) { - g_slist_foreach (routes, (GFunc) nm_ip4_route_unref, NULL); - g_slist_free (routes); - } + if (routes) + g_slist_free_full (routes, (GDestroyNotify) nm_ip4_route_unref); return items; } @@ -289,10 +285,8 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix) } if (num) items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ADDRESSES=%d", prefix, num)); - if (addresses) { - g_slist_foreach (addresses, (GFunc) nm_ip6_address_unref, NULL); - g_slist_free (addresses); - } + if (addresses) + g_slist_free_full (addresses, (GDestroyNotify) nm_ip6_address_unref); /* DNS servers */ val = g_hash_table_lookup (ip6_config, "nameservers"); @@ -352,10 +346,8 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix) } if (num) items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ROUTES=%d", prefix, num)); - if (routes) { - g_slist_foreach (routes, (GFunc) nm_ip6_route_unref, NULL); - g_slist_free (routes); - } + if (routes) + g_slist_free_full (routes, (GDestroyNotify) nm_ip6_route_unref); return items; } |