From b76bf3ca72460a06ca2e5dd19eb3e5729487d270 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 14 Dec 2011 16:28:56 +0000 Subject: [PATCH 1/2] gobject: Add a g_assert_finalize_object() macro With modifications by Philip Withnall. Fixes: #488 --- docs/reference/gobject/gobject-sections.txt | 3 ++ gobject/gobject.h | 32 +++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/docs/reference/gobject/gobject-sections.txt b/docs/reference/gobject/gobject-sections.txt index 2247888d1..bed38e4c5 100644 --- a/docs/reference/gobject/gobject-sections.txt +++ b/docs/reference/gobject/gobject-sections.txt @@ -322,6 +322,9 @@ g_weak_ref_clear g_weak_ref_get g_weak_ref_set + +g_assert_finalize_object + G_INITIALLY_UNOWNED G_INITIALLY_UNOWNED_CLASS diff --git a/gobject/gobject.h b/gobject/gobject.h index 12e0882c6..91b9f6328 100644 --- a/gobject/gobject.h +++ b/gobject/gobject.h @@ -744,6 +744,38 @@ static inline gboolean (g_set_object) ((GObject **) (object_ptr), (GObject *) (new_object)) \ ) +/** + * g_assert_finalize_object: (skip) + * @object: (transfer full) (type GObject.Object): an object + * + * Assert that @object is non-%NULL, then release one reference to it with + * g_object_unref() and assert that it has been finalized (i.e. that there + * are no more references). + * + * If assertions are disabled via `G_DISABLE_ASSERT`, + * this macro just calls g_object_unref() without any further checks. + * + * This macro should only be used in regression tests. + * + * Since: 2.62 + */ +static inline void +(g_assert_finalize_object) (GObject *object) +{ + gpointer weak_pointer = object; + + g_assert_true (G_IS_OBJECT (weak_pointer)); + g_object_add_weak_pointer (object, &weak_pointer); + g_object_unref (weak_pointer); + g_assert_null (weak_pointer); +} + +#ifdef G_DISABLE_ASSERT +#define g_assert_finalize_object(object) g_object_unref (object) +#else +#define g_assert_finalize_object(object) (g_assert_finalize_object ((GObject *) object)) +#endif + /** * g_clear_weak_pointer: (skip) * @weak_pointer_location: The memory address of a pointer From b6d4da7684cafe62060066bee64f14a3d6fd4545 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 21 May 2019 12:53:21 +0100 Subject: [PATCH 2/2] tests: Use g_object_assert_last_unref() in various tests Signed-off-by: Philip Withnall --- gio/tests/glistmodel.c | 4 +--- gio/tests/tls-interaction.c | 10 +--------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/gio/tests/glistmodel.c b/gio/tests/glistmodel.c index 2fef4ccbe..b867bd54a 100644 --- a/gio/tests/glistmodel.c +++ b/gio/tests/glistmodel.c @@ -77,7 +77,6 @@ test_store_boundaries (void) store = g_list_store_new (G_TYPE_MENU_ITEM); item = g_menu_item_new (NULL, NULL); - g_object_add_weak_pointer (G_OBJECT (item), (gpointer *) &item); /* remove an item from an empty list */ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*g_sequence*"); @@ -123,8 +122,7 @@ test_store_boundaries (void) g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 1); g_object_unref (store); - g_object_unref (item); - g_assert_null (item); + g_assert_finalize_object (item); } static void diff --git a/gio/tests/tls-interaction.c b/gio/tests/tls-interaction.c index 345656e29..4f0737d7e 100644 --- a/gio/tests/tls-interaction.c +++ b/gio/tests/tls-interaction.c @@ -673,18 +673,10 @@ static void teardown_without_loop (Test *test, gconstpointer unused) { - gpointer weak_pointer = test->interaction; - - g_object_add_weak_pointer (weak_pointer, &weak_pointer); - g_object_unref (test->connection); - g_object_unref (test->password); - g_object_unref (test->interaction); - - g_assert (weak_pointer == NULL); - + g_assert_finalize_object (test->interaction); } typedef struct {