gobject/tests: add test checking that GWeakRef is cleared in GWeakNotify

g_object_weak_ref() documentation refers to GWeakRef as thread-safe
replacement.  However, it's not clear to me, how GWeakRef is a
replacement for a callback. I think, it means, that you combine
g_object_weak_ref() with GWeakRef, to both hold a (thread-safe) weak
reference and get a notification on destruction.

Add a test, that GWeakRef is already cleared inside the GWeakNotify
callback.
This commit is contained in:
Thomas Haller 2024-01-31 12:11:07 +01:00
parent 936bb9ecfb
commit 970325b92d

View File

@ -105,6 +105,17 @@ weak_ref2 (gpointer data,
weak_ref2_notified = TRUE;
}
static void
weak_ref3 (gpointer data,
GObject *object)
{
GWeakRef *weak_ref = data;
g_assert_null (g_weak_ref_get (weak_ref));
weak_ref2_notified = TRUE;
}
static void
toggle_ref1 (gpointer data,
GObject *object,
@ -154,6 +165,7 @@ static void
test_references (void)
{
GObject *object;
GWeakRef weak_ref;
/* Test basic weak reference operation */
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);
@ -191,6 +203,17 @@ test_references (void)
g_assert_true (weak_ref2_notified);
g_assert_true (object_destroyed);
/* Test that within a GWeakNotify the GWeakRef is NULL already. */
weak_ref2_notified = FALSE;
object = g_object_new (G_TYPE_OBJECT, NULL);
g_weak_ref_init (&weak_ref, object);
g_assert_true (object == g_weak_ref_get (&weak_ref));
g_object_weak_ref (object, weak_ref3, &weak_ref);
g_object_unref (object);
g_object_unref (object);
g_assert_true (weak_ref2_notified);
g_weak_ref_clear (&weak_ref);
/* Test basic toggle reference operation */
global_object = object = g_object_new (TEST_TYPE_OBJECT, NULL);