gobject: clear weak locations before calling dispose in g_object_run_dispose()

This changes behavior from commit [1].

The point of g_object_run_dispose() is to break reference cycles to
bring down an object. We don't expect the object to take new references
to keep it alive for longer. We probably also don't expect it to
register new weak references. We also don't expect the dispose()
callees to check g_weak_ref_get() for the object. In those situations,
this change makes no difference.

Note that during g_object_run_dispose() the ref count does not yet go to
zero, still we clear GWeakRef. As such, GWeakRef rather tracks when
objects get disposed, instead of when the ref count goes to zero. That
is intentional (e.g. issue [2]).

But compare to g_object_unref(), where we also clear GWeakRef *before*
calling dispose. That makes sense, because inside dispose() (and for
example during weak notifications), we probably want to see that
g_weak_ref_get() indicates the object is being disposed. For that
reason, it seems more correct to clear out the GWeakRef before calling
dispose().

Also, the dispose() callees (e.g. the weak notifications) might refuse to
let the object die by intentionally keeping strong references around.
Not sure why they would do that, it is similar to resurrecting an object
during dispose(). But if they do, they might also want to register new
GWeakRef. In that case, we wouldn't want to unset those newly set
GWeakRef unconditionally right after.

In most cases, it shouldn't make a difference. In the case where it
does, this seems the more sensible order of things.

[1] commit 2952cfd7a7 ('gobject: drop clearing quark_weak_locations from g_object_real_dispose()')
[2] https://gitlab.gnome.org/GNOME/glib/-/issues/2266
This commit is contained in:
Thomas Haller
2025-04-09 21:24:33 +02:00
parent 1b1e03c5d4
commit 5b7e48261c

View File

@@ -1858,6 +1858,15 @@ g_object_dispatch_properties_changed (GObject *object,
* reference cycles.
*
* This function should only be called from object system implementations.
*
* This function temporarily acquires another strong reference while running
* dispose.
*
* Note that this first clears all #GWeakRef pointers and then calls
* #GObjectClass.dispose.
*
* Note that before 2.86, #GWeakRef pointers were cleared after
* #GObjectClass.dispose.
*/
void
g_object_run_dispose (GObject *object)
@@ -1869,10 +1878,6 @@ g_object_run_dispose (GObject *object)
g_object_ref (object);
TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
G_OBJECT_GET_CLASS (object)->dispose (object);
TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
if ((object_get_optional_flags (object) & OPTIONAL_FLAG_EVER_HAD_WEAK_REF))
{
wrdata = weak_ref_data_get_surely (object);
@@ -1881,6 +1886,10 @@ g_object_run_dispose (GObject *object)
weak_ref_data_unlock (wrdata);
}
TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
G_OBJECT_GET_CLASS (object)->dispose (object);
TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
g_object_unref (object);
}
@@ -5548,8 +5557,7 @@ g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
* atomic with respect to invalidation of weak pointers to destroyed
* objects.
*
* #GWeakRefs are reset before calling #GObjectClass.dispose (after in case of
* #GObjectClass.run_dispose()).
* #GWeakRefs are reset before calling #GObjectClass.dispose.
* If the object's #GObjectClass.dispose method results in additional
* references to the object being held (re-referencing), any #GWeakRefs taken
* before it was disposed will continue to point to %NULL. If during disposal
@@ -5558,6 +5566,10 @@ g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
* #GObjectClass.dispose is called again. If #GWeakRefs were taken during
* disposal but the object not resurrected, they will be set to %NULL right
* after, before finalization.
*
* Note that #GObjectClass.run_dispose() also resets #GWeakRefs. As such,
* the #GWeakRef really tracks whether #GObjectClass.dispose() was called
* and not the ref count reaching zero.
*/
#define WEAK_REF_LOCK_BIT 0