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>
This commit is contained in:
Thomas Haller 2013-10-18 16:07:26 +02:00
parent 1c93b24829
commit 3eb1d5e902
26 changed files with 69 additions and 108 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -389,7 +389,7 @@ nmc_connection_detail (NMConnection *connection, NmCli *nmc)
}
static void
fill_output_connection (NMConnection *data, gpointer user_data)
fill_output_connection (gpointer data, gpointer user_data)
{
NMConnection *connection = (NMConnection *) data;
NmCli *nmc = (NmCli *) user_data;
@ -502,7 +502,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
g_ptr_array_add (nmc->output_data, arr);
/* Add values */
g_slist_foreach (nmc->system_connections, (GFunc) fill_output_connection, nmc);
g_slist_foreach (nmc->system_connections, fill_output_connection, nmc);
print_data (nmc); /* Print all data */
} else {
g_clear_error (&error1); /* the error1 is only relevant for 'show configured' without arguments */

View file

@ -67,7 +67,7 @@ setup_signals (void)
/* Print details of connection */
static void
show_connection (NMConnection *data, gpointer user_data)
show_connection (gpointer data, gpointer user_data)
{
NMConnection *connection = (NMConnection *) data;
NMSettingConnection *s_con;
@ -107,7 +107,7 @@ get_connections_cb (NMRemoteSettings *settings, gpointer user_data)
printf ("Connections:\n===================\n");
g_slist_foreach (connections, (GFunc) show_connection, NULL);
g_slist_foreach (connections, show_connection, NULL);
g_slist_free (connections);
g_object_unref (settings);

View file

@ -456,8 +456,7 @@ _libnm_glib_ctx_free (libnm_glib_ctx *ctx)
if (ctx->callbacks_lock)
g_mutex_free (ctx->callbacks_lock);
g_slist_foreach (ctx->callbacks, (GFunc)g_free, NULL);
g_slist_free (ctx->callbacks);
g_slist_free_full (ctx->callbacks, g_free);
if (ctx->thread)
g_thread_join (ctx->thread);

View file

@ -354,7 +354,7 @@ dispose (GObject *object)
NMActiveConnectionPrivate *priv = NM_ACTIVE_CONNECTION_GET_PRIVATE (object);
if (priv->devices) {
g_ptr_array_foreach (priv->devices, (GFunc) g_object_unref, NULL);
g_ptr_array_set_free_func (priv->devices, g_object_unref);
g_ptr_array_free (priv->devices, TRUE);
priv->devices = NULL;
}

View file

@ -2034,8 +2034,7 @@ dispose (GObject *object)
g_clear_object (&priv->primary_connection);
g_clear_object (&priv->activating_connection);
g_slist_foreach (priv->pending_activations, (GFunc) activate_info_free, NULL);
g_slist_free (priv->pending_activations);
g_slist_free_full (priv->pending_activations, (GDestroyNotify) activate_info_free);
priv->pending_activations = NULL;
g_hash_table_destroy (priv->permissions);

View file

@ -238,7 +238,7 @@ dispose (GObject *object)
g_clear_object (&priv->proxy);
if (priv->slaves) {
g_ptr_array_foreach (priv->slaves, (GFunc) g_object_unref, NULL);
g_ptr_array_set_free_func (priv->slaves, g_object_unref);
g_ptr_array_free (priv->slaves, TRUE);
priv->slaves = NULL;
}

View file

@ -246,7 +246,7 @@ dispose (GObject *object)
g_clear_object (&priv->proxy);
if (priv->slaves) {
g_ptr_array_foreach (priv->slaves, (GFunc) g_object_unref, NULL);
g_ptr_array_set_free_func (priv->slaves, g_object_unref);
g_ptr_array_free (priv->slaves, TRUE);
priv->slaves = NULL;
}

View file

@ -234,7 +234,7 @@ dispose (GObject *object)
g_clear_object (&priv->proxy);
if (priv->slaves) {
g_ptr_array_foreach (priv->slaves, (GFunc) g_object_unref, NULL);
g_ptr_array_set_free_func (priv->slaves, g_object_unref);
g_ptr_array_free (priv->slaves, TRUE);
priv->slaves = NULL;
}

View file

@ -69,8 +69,7 @@ demarshal_ip4_address_array (NMObject *object, GParamSpec *pspec, GValue *value,
{
NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object);
g_slist_foreach (priv->addresses, (GFunc) nm_ip4_address_unref, NULL);
g_slist_free (priv->addresses);
g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref);
priv->addresses = NULL;
priv->addresses = nm_utils_ip4_addresses_from_gvalue (value);
@ -108,8 +107,7 @@ demarshal_ip4_routes_array (NMObject *object, GParamSpec *pspec, GValue *value,
{
NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object);
g_slist_foreach (priv->routes, (GFunc) nm_ip4_route_unref, NULL);
g_slist_free (priv->routes);
g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref);
priv->routes = NULL;
priv->routes = nm_utils_ip4_routes_from_gvalue (value);
@ -156,11 +154,8 @@ finalize (GObject *object)
g_free (priv->gateway);
g_slist_foreach (priv->addresses, (GFunc) nm_ip4_address_unref, NULL);
g_slist_free (priv->addresses);
g_slist_foreach (priv->routes, (GFunc) nm_ip4_route_unref, NULL);
g_slist_free (priv->routes);
g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref);
g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref);
if (priv->nameservers)
g_array_free (priv->nameservers, TRUE);
@ -169,12 +164,12 @@ finalize (GObject *object)
g_array_free (priv->wins, TRUE);
if (priv->domains) {
g_ptr_array_foreach (priv->domains, (GFunc) g_free, NULL);
g_ptr_array_set_free_func (priv->domains, g_free);
g_ptr_array_free (priv->domains, TRUE);
}
if (priv->searches) {
g_ptr_array_foreach (priv->searches, (GFunc) g_free, NULL);
g_ptr_array_set_free_func (priv->searches, g_free);
g_ptr_array_free (priv->searches, TRUE);
}

View file

@ -80,8 +80,7 @@ demarshal_ip6_address_array (NMObject *object, GParamSpec *pspec, GValue *value,
{
NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object);
g_slist_foreach (priv->addresses, (GFunc) nm_ip6_address_unref, NULL);
g_slist_free (priv->addresses);
g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip6_address_unref);
priv->addresses = NULL;
priv->addresses = nm_utils_ip6_addresses_from_gvalue (value);
@ -127,8 +126,7 @@ demarshal_ip6_routes_array (NMObject *object, GParamSpec *pspec, GValue *value,
{
NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object);
g_slist_foreach (priv->routes, (GFunc) nm_ip6_route_unref, NULL);
g_slist_free (priv->routes);
g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref);
priv->routes = NULL;
priv->routes = nm_utils_ip6_routes_from_gvalue (value);
@ -289,22 +287,17 @@ finalize (GObject *object)
g_free (priv->gateway);
g_slist_foreach (priv->addresses, (GFunc) nm_ip6_address_unref, NULL);
g_slist_free (priv->addresses);
g_slist_foreach (priv->routes, (GFunc) nm_ip6_route_unref, NULL);
g_slist_free (priv->routes);
g_slist_foreach (priv->nameservers, (GFunc) g_free, NULL);
g_slist_free (priv->nameservers);
g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip6_address_unref);
g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref);
g_slist_free_full (priv->nameservers, g_free);
if (priv->domains) {
g_ptr_array_foreach (priv->domains, (GFunc) g_free, NULL);
g_ptr_array_set_free_func (priv->domains, g_free);
g_ptr_array_free (priv->domains, TRUE);
}
if (priv->searches) {
g_ptr_array_foreach (priv->searches, (GFunc) g_free, NULL);
g_ptr_array_set_free_func (priv->searches, g_free);
g_ptr_array_free (priv->searches, TRUE);
}

View file

@ -317,12 +317,10 @@ dispose (GObject *object)
priv->notify_id = 0;
}
g_slist_foreach (priv->notify_props, (GFunc) g_free, NULL);
g_slist_free (priv->notify_props);
g_slist_free_full (priv->notify_props, g_free);
priv->notify_props = NULL;
g_slist_foreach (priv->property_interfaces, (GFunc) g_free, NULL);
g_slist_free (priv->property_interfaces);
g_slist_free_full (priv->property_interfaces, g_free);
priv->property_interfaces = NULL;
g_clear_object (&priv->properties_proxy);
@ -341,8 +339,7 @@ finalize (GObject *object)
{
NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (object);
g_slist_foreach (priv->property_tables, (GFunc) g_hash_table_destroy, NULL);
g_slist_free (priv->property_tables);
g_slist_free_full (priv->property_tables, (GDestroyNotify) g_hash_table_destroy);
g_free (priv->path);
if (priv->pseudo_properties)

View file

@ -363,8 +363,7 @@ _nm_ip6_address_array_demarshal (GValue *value, GSList **dest)
return FALSE;
if (*dest) {
g_slist_foreach (*dest, (GFunc) g_free, NULL);
g_slist_free (*dest);
g_slist_free_full (*dest, g_free);
*dest = NULL;
}

View file

@ -291,10 +291,8 @@ _gvalues_compare_collection (const GValue *value1, const GValue *value2)
ret = _gvalues_compare ((GValue *) iter1->data, (GValue *) iter2->data);
}
g_slist_foreach (list1, (GFunc) _gvalue_destroy, NULL);
g_slist_free (list1);
g_slist_foreach (list2, (GFunc) _gvalue_destroy, NULL);
g_slist_free (list2);
g_slist_free_full (list1, _gvalue_destroy);
g_slist_free_full (list2, _gvalue_destroy);
}
return ret;

View file

@ -267,8 +267,7 @@ foreach_item_helper (GHashTable *hash,
func (siter->data, value, user_data);
}
g_slist_foreach (copied, (GFunc) g_free, NULL);
g_slist_free (copied);
g_slist_free_full (copied, g_free);
}
/**

View file

@ -674,7 +674,7 @@ finalize (GObject *object)
g_slist_free_full (priv->mac_address_blacklist, g_free);
if (priv->s390_subchannels) {
g_ptr_array_foreach (priv->s390_subchannels, (GFunc) g_free, NULL);
g_ptr_array_set_free_func (priv->s390_subchannels, g_free);
g_ptr_array_free (priv->s390_subchannels, TRUE);
}
@ -728,7 +728,7 @@ set_property (GObject *object, guint prop_id,
break;
case PROP_S390_SUBCHANNELS:
if (priv->s390_subchannels) {
g_ptr_array_foreach (priv->s390_subchannels, (GFunc) g_free, NULL);
g_ptr_array_set_free_func (priv->s390_subchannels, g_free);
g_ptr_array_free (priv->s390_subchannels, TRUE);
}
priv->s390_subchannels = g_value_dup_boxed (value);

View file

@ -6412,13 +6412,11 @@ ip4_match_config (NMDevice *self, NMConnection *connection)
break;
}
}
g_slist_foreach (leases, (GFunc) g_object_unref, NULL);
g_slist_free (leases);
g_slist_free_full (leases, g_object_unref);
return found;
} else {
/* Maybe the connection used to be DHCP and there are stale leases; ignore them */
g_slist_foreach (leases, (GFunc) g_object_unref, NULL);
g_slist_free (leases);
g_slist_free_full (leases, g_object_unref);
}
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) {

View file

@ -1349,8 +1349,7 @@ dispose (GObject *object)
set_current_nsp (self, NULL);
g_slist_foreach (priv->nsp_list, (GFunc) g_object_unref, NULL);
g_slist_free (priv->nsp_list);
g_slist_free_full (priv->nsp_list, g_object_unref);
iwmx_sdk_new_callback_unregister (wmx_new_sdk_cb, self);
nm_wimax_util_sdk_unref ();

View file

@ -214,8 +214,10 @@ private_server_new (const char *path,
}
static void
private_server_free (PrivateServer *s)
private_server_free (gpointer ptr)
{
PrivateServer *s = ptr;
unlink (s->address);
g_free (s->address);
g_free (s->tag);
@ -472,8 +474,7 @@ nm_dbus_manager_dispose (GObject *object)
priv->exported = NULL;
}
g_slist_foreach (priv->private_servers, (GFunc) private_server_free, NULL);
g_slist_free (priv->private_servers);
g_slist_free_full (priv->private_servers, private_server_free);
priv->private_servers = NULL;
priv->priv_server = NULL;

View file

@ -4501,8 +4501,7 @@ dispose (GObject *object)
}
priv->disposed = TRUE;
g_slist_foreach (priv->auth_chains, (GFunc) nm_auth_chain_unref, NULL);
g_slist_free (priv->auth_chains);
g_slist_free_full (priv->auth_chains, (GDestroyNotify) nm_auth_chain_unref);
nm_auth_changed_func_unregister (authority_changed_cb, manager);

View file

@ -509,8 +509,7 @@ dispose (GObject *object)
g_free (priv->identifier);
g_free (priv->owner_username);
g_slist_foreach (priv->permissions, (GFunc) g_free, NULL);
g_slist_free (priv->permissions);
g_slist_free_full (priv->permissions, g_free);
g_hash_table_destroy (priv->requests);

View file

@ -361,8 +361,7 @@ clear_unmanaged_specs (NMSettings *self)
{
NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
g_slist_foreach (priv->unmanaged_specs, (GFunc) g_free, NULL);
g_slist_free (priv->unmanaged_specs);
g_slist_free_full (priv->unmanaged_specs, g_free);
priv->unmanaged_specs = NULL;
}
@ -658,8 +657,7 @@ load_plugins (NMSettings *self, const char **plugins, GError **error)
if (!keyfile_added)
add_keyfile_plugin (self);
g_slist_foreach (list, (GFunc) g_object_unref, NULL);
g_slist_free (list);
g_slist_free_full (list, g_object_unref);
return success;
}
@ -1732,11 +1730,9 @@ dispose (GObject *object)
{
NMSettings *self = NM_SETTINGS (object);
NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
GSList *iter;
for (iter = priv->auths; iter; iter = g_slist_next (iter))
nm_auth_chain_unref ((NMAuthChain *) iter->data);
g_slist_free (priv->auths);
g_slist_free_full (priv->auths, (GDestroyNotify) nm_auth_chain_unref);
priv->auths = NULL;
priv->dbus_mgr = NULL;
@ -1757,8 +1753,7 @@ finalize (GObject *object)
clear_unmanaged_specs (self);
g_slist_foreach (priv->plugins, (GFunc) g_object_unref, NULL);
g_slist_free (priv->plugins);
g_slist_free_full (priv->plugins, g_object_unref);
G_OBJECT_CLASS (nm_settings_parent_class)->finalize (object);
}

View file

@ -392,8 +392,7 @@ svCloseFile(shvarFile *s)
g_free(s->arena);
g_free(s->fileName);
g_list_foreach (s->lineList, (GFunc) g_free, NULL);
g_list_free(s->lineList); /* implicitly frees s->current */
g_list_free_full (s->lineList, g_free); /* implicitly frees s->current */
g_free(s);
return 0;
}

View file

@ -240,9 +240,7 @@ string_to_glist_of_strings(const gchar* data)
static void
slist_free_all(gpointer slist)
{
GSList *list = (GSList *) slist;
g_slist_foreach (list, (GFunc) g_free, NULL);
g_slist_free (list);
g_slist_free_full ((GSList *) slist, g_free);
}
static void

View file

@ -57,8 +57,10 @@ expected_key_new (const char *key, const char *data)
}
static void
expected_key_free (ExpectedKey *k)
expected_key_free (gpointer ptr)
{
ExpectedKey *k = ptr;
g_assert (k);
g_free (k->key);
g_free (k->data);
@ -81,11 +83,12 @@ expected_block_new (const char *type, const char *name)
}
static void
expected_block_free (ExpectedBlock *b)
expected_block_free (gpointer ptr)
{
ExpectedBlock *b = ptr;
g_assert (b);
g_slist_foreach (b->keys, (GFunc) expected_key_free, NULL);
g_slist_free (b->keys);
g_slist_free_full (b->keys, expected_key_free);
g_free (b->type);
g_free (b->name);
memset (b, 0, sizeof (ExpectedBlock));
@ -122,8 +125,7 @@ static void
expected_free (Expected *e)
{
g_assert (e);
g_slist_foreach (e->blocks, (GFunc) expected_block_free, NULL);
g_slist_free (e->blocks);
g_slist_free_full (e->blocks, expected_block_free);
memset (e, 0, sizeof (Expected));
g_free (e);
}