summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Trevisan (Treviño) <mail@3v1n0.net>2021-09-15 22:13:28 +0200
committerMarco Trevisan (Treviño) <mail@3v1n0.net>2021-09-17 12:27:59 +0200
commite861f60dcbfc93b46623df8fccc9ce12c7f9d1e1 (patch)
tree400bc92a574a3adce830ea25caedc77a781c73ac
parentea68b22135f6ca41b9d520ab169ae2346e29d276 (diff)
downloadglib-e861f60dcbfc93b46623df8fccc9ce12c7f9d1e1.tar.gz
gobject: Cleanup weak locations when the last one has been removed
As per the previous change, an object that had weak locations set may need to lock again the weak locations mutex during qdata cleanup, but we can avoid this when we know we're removing the last location, by removing the qdata entry and freeing the data. In case a new location is needed for the same object, new data will be added. However, by doing this the weak locations during dispose may be invalidated once the weak locations lock is passed, so check again if this is the case while removing them.
-rw-r--r--gobject/gobject.c13
-rw-r--r--gobject/tests/reference.c12
2 files changed, 22 insertions, 3 deletions
diff --git a/gobject/gobject.c b/gobject/gobject.c
index c558c1197..4f4a463ef 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -3536,10 +3536,11 @@ g_object_unref (gpointer _object)
}
/* We got the lock first, so the object will definitely die
- * now. Clear out all the weak references.
+ * now. Clear out all the weak references, if they're still set.
*/
- weak_locations_free_unlocked (weak_locations);
- g_datalist_id_remove_no_notify (&object->qdata, quark_weak_locations);
+ weak_locations = g_datalist_id_remove_no_notify (&object->qdata,
+ quark_weak_locations);
+ g_clear_pointer (&weak_locations, weak_locations_free_unlocked);
g_rw_lock_writer_unlock (&weak_locations_lock);
}
@@ -4731,6 +4732,12 @@ g_weak_ref_set (GWeakRef *weak_ref,
g_assert (weak_locations != NULL);
*weak_locations = g_slist_remove (*weak_locations, weak_ref);
+
+ if (!*weak_locations)
+ {
+ weak_locations_free_unlocked (weak_locations);
+ g_datalist_id_remove_no_notify (&old_object->qdata, quark_weak_locations);
+ }
}
/* Add the weak ref to the new object */
diff --git a/gobject/tests/reference.c b/gobject/tests/reference.c
index de49ff1fd..e84c66467 100644
--- a/gobject/tests/reference.c
+++ b/gobject/tests/reference.c
@@ -558,6 +558,18 @@ test_weak_ref (void)
g_weak_ref_clear (&weak3);
+ /* unset dynamic_weak... */
+ g_weak_ref_set (dynamic_weak, NULL);
+ g_assert_null (g_weak_ref_get (dynamic_weak));
+
+ /* initializing a weak reference to an object that had before works */
+ g_weak_ref_set (dynamic_weak, obj2);
+ tmp = g_weak_ref_get (dynamic_weak);
+ g_assert_true (tmp == obj2);
+ g_assert_cmpint (obj2->ref_count, ==, 2);
+ g_object_unref (tmp);
+ g_assert_cmpint (obj2->ref_count, ==, 1);
+
/* clear and free dynamic_weak... */
g_weak_ref_clear (dynamic_weak);