Fix secret_schema_unref detection of 0 refcount

g_atomic_int_add (&schema->refs, -1); will return the value of 'refs'
_before_ adding -1 to it, so checking this value for 0 to see if the
refcount dropped to 0 after adding -1 is not going to work and will
cause a leak.
Using g_atomic_int_dec_and_test() fixes this problem as this will return
TRUE when the value drops to 0 after being decremented.

https://bugzilla.gnome.org/show_bug.cgi?id=756766
This commit is contained in:
Christophe Fergeau 2015-07-26 22:36:20 +02:00
parent 20f35bfd92
commit 1b55291feb

View File

@ -355,16 +355,12 @@ _secret_schema_ref_if_nonstatic (const SecretSchema *schema)
void void
secret_schema_unref (SecretSchema *schema) secret_schema_unref (SecretSchema *schema)
{ {
gint refs;
gint i;
g_return_if_fail (schema != NULL); g_return_if_fail (schema != NULL);
/* statically-allocated or invalid SecretSchema */
g_return_if_fail (g_atomic_int_get (&schema->reserved) > 0);
refs = g_atomic_int_add (&schema->reserved, -1); if (g_atomic_int_dec_and_test (&schema->reserved)) {
if (refs < 0) { gint i;
g_warning ("should not unreference a static or invalid SecretSchema");
} else if (refs == 0) {
g_free ((gpointer)schema->name); g_free ((gpointer)schema->name);
for (i = 0; i < G_N_ELEMENTS (schema->attributes); i++) for (i = 0; i < G_N_ELEMENTS (schema->attributes); i++)
g_free ((gpointer)schema->attributes[i].name); g_free ((gpointer)schema->attributes[i].name);