Add destructor for GParamSpecPool

While GParamSpecPool should never be used by newly written code, having
the ability to free the associated memory is a good idea. The only
reason why this hasn't been necessary until now is that we assume base
classes are going to keep their GParamSpecPool around forever.
This commit is contained in:
Emmanuele Bassi 2023-12-14 14:59:43 +00:00
parent 989f87d26a
commit 8ce40ac590
2 changed files with 25 additions and 5 deletions

View File

@ -994,11 +994,32 @@ g_param_spec_pool_new (gboolean type_prefixing)
memcpy (&pool->mutex, &init_mutex, sizeof (init_mutex));
pool->type_prefixing = type_prefixing != FALSE;
pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
pool->hash_table = g_hash_table_new_full (param_spec_pool_hash,
param_spec_pool_equals,
(GDestroyNotify) g_param_spec_unref,
NULL);
return pool;
}
/**
* g_param_spec_pool_free:
* @pool: (transfer full): a #GParamSpecPool
*
* Frees the resources allocated by a #GParamSpecPool.
*
* Since: 2.80
*/
void
g_param_spec_pool_free (GParamSpecPool *pool)
{
g_mutex_lock (&pool->mutex);
g_hash_table_unref (pool->hash_table);
g_mutex_unlock (&pool->mutex);
g_mutex_clear (&pool->mutex);
g_free (pool);
}
/**
* g_param_spec_pool_insert:
* @pool: a #GParamSpecPool.
@ -1053,9 +1074,7 @@ g_param_spec_pool_remove (GParamSpecPool *pool,
if (pool && pspec)
{
g_mutex_lock (&pool->mutex);
if (g_hash_table_remove (pool->hash_table, pspec))
g_param_spec_unref (pspec);
else
if (!g_hash_table_remove (pool->hash_table, pspec))
g_critical (G_STRLOC ": attempt to remove unknown pspec '%s' from pool", pspec->name);
g_mutex_unlock (&pool->mutex);
}

View File

@ -443,7 +443,8 @@ GOBJECT_AVAILABLE_IN_ALL
GParamSpec** g_param_spec_pool_list (GParamSpecPool *pool,
GType owner_type,
guint *n_pspecs_p);
GOBJECT_AVAILABLE_IN_2_80
void g_param_spec_pool_free (GParamSpecPool *pool);
/* contracts:
*