gibaseinfo: Break refcount cycle with GIRepository

By shifting responsibility for ensuring that the lifetime of a
`GIRepository` always exceeds the lifetime of any of its `GIBaseInfo`s
to the user.

Keeping a weak ref from each `GIBaseInfo` to its `GIRepository` would be
too expensive (`GIBaseInfo`s are supposed to be cheap to create and
destroy, as they are used within function calls in language bindings).

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Fixes: #3234
This commit is contained in:
Philip Withnall
2024-01-25 22:03:57 +00:00
parent 01ef6bb49e
commit 7eb69d279d
3 changed files with 20 additions and 5 deletions

View File

@@ -126,9 +126,6 @@ gi_base_info_finalize (GIBaseInfo *self)
if (self->ref_count != INVALID_REFCOUNT &&
self->container && self->container->ref_count != INVALID_REFCOUNT)
gi_base_info_unref (self->container);
if (self->ref_count != INVALID_REFCOUNT)
g_clear_object (&self->repository);
}
static void
@@ -356,7 +353,14 @@ gi_info_new_full (GIInfoType type,
if (container && container->ref_count != INVALID_REFCOUNT)
gi_base_info_ref (info->container);
info->repository = g_object_ref (repository);
/* Dont keep a strong ref, since the repository keeps a cache of #GIBaseInfos
* and holds refs on them. If we kept a ref here, thered be a cycle.
* Dont keep a weak ref either, as that would make creating/destroying a
* #GIBaseInfo noticeably more expensive, and infos are performance critical
* for bindings.
* As stated in the documentation, the mitigation here is to require the user
* to keep the #GIRepository alive longer than any of its #GIBaseInfos. */
info->repository = repository;
return (GIBaseInfo*)info;
}