Skip to content

fix: release transfer-full GObject return refs so they can be GC'd#456

Merged
romgrk merged 1 commit into
masterfrom
fix/446-gobject-gi-return-leak
Jun 23, 2026
Merged

fix: release transfer-full GObject return refs so they can be GC'd#456
romgrk merged 1 commit into
masterfrom
fix/446-gobject-gi-return-leak

Conversation

@romgrk

@romgrk romgrk commented Jun 23, 2026

Copy link
Copy Markdown
Owner

Fixes #446.

Problem

GObjects returned from GI functions — constructor helpers like Type.new(), and transfer-full returns generally — were never collected, even after every JS reference was dropped and GC was forced repeatedly. The same type built with new Type() was collected correctly, so the leak was specific to the GI-return path. The reporter hit a monotonically growing live heap (and increasingly long major-GC pauses) from a git poll creating Ggit objects via .new() on a timer.

construction created alive after dropping all refs + GC×8
Gio.Menu.new() (GI function) 10000 100012 (fixed)
new Gio.Menu() (JS constructor) 10000 2

Root cause

A transfer-full return hands node-gtk an owning reference on top of the wrapper's toggle ref. node-gtk only needs the toggle ref, so the extra reference must be released; otherwise the refcount never falls back to 1, ToggleNotify never flips the V8 handle to weak (SetWeak), and the object (and its wrapper) is pinned alive for the lifetime of the process.

  • new Type() works because GObjectConstructor drops its construction ref.
  • OUT GObject args work because FreeGIArgument already drops their transfer-full ref.
  • The return value is never freed on the success path, so it leaked 1:1 and unbounded.

Fix

After wrapping the return value, drop the extra owning reference for a transfer-full GObject return — the return-value counterpart of the OUT-arg balance and of RefObjectForTransferFullIn (the #439 IN-side fix).

Ownership is sampled before wrapping, since associating the wrapper sinks any floating reference. The helper guards with G_IS_OBJECT (excludes GParamSpec / boxed / fundamental interface types) and !g_object_is_floating (a sunk floating ref is nothing extra to drop), so floating widgets and transfer-none returns are left untouched.

Verification

  • Issue's exact repro: Gio.Menu.new() goes from 10001 alive → 2 alive, matching new Gio.Menu().
  • New regression test tests/object__gi_return_leak.js (creates many Gio.Menu.new(), drops refs, GCs, asserts the wrappers are reclaimed via FinalizationRegistry).
  • Full suite: 84 passing including the new test. The only failure (require.js) is a pre-existing, environment-specific typelib conflict (PeasGtk / GIRepository 3.0 vs 2.0) that fails identically on unmodified master.

🤖 Generated with Claude Code

GObjects handed back from GI function returns (constructor helpers like
`Type.new()`, and transfer-full returns generally) were never collected,
even after every JS reference was dropped and GC forced. The same type
built with `new Type()` was collected correctly.

A transfer-full return hands node-gtk an owning reference on top of the
wrapper's toggle ref. node-gtk only needs the toggle ref, so the extra
reference must be released; otherwise the refcount never falls back to 1,
ToggleNotify never flips the V8 handle to weak, and the object (and its
wrapper) is pinned alive for the lifetime of the process. OUT GObject args
were already balanced by FreeGIArgument, but the return value is never
freed on the success path, so it leaked 1:1 and unbounded.

Drop the extra reference after wrapping, mirroring the OUT-arg balance and
the `new Type()` path's construction-ref drop. Ownership is sampled before
wrapping, since associating the wrapper sinks any floating reference. This
is the return-value counterpart of RefObjectForTransferFullIn (#439).

Fixes #446

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@romgrk romgrk merged commit 3300ed0 into master Jun 23, 2026
9 checks passed
@romgrk romgrk deleted the fix/446-gobject-gi-return-leak branch June 23, 2026 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GObjects returned from GI functions (Type.new()) are never GC'd — leak vs new Type()

1 participant