fix: release transfer-full GObject return refs so they can be GC'd#456
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withnew 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.Gio.Menu.new()(GI function)new Gio.Menu()(JS constructor)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,
ToggleNotifynever 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 becauseGObjectConstructordrops its construction ref.FreeGIArgumentalready drops their transfer-full ref.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(excludesGParamSpec/ 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
Gio.Menu.new()goes from 10001 alive → 2 alive, matchingnew Gio.Menu().tests/object__gi_return_leak.js(creates manyGio.Menu.new(), drops refs, GCs, asserts the wrappers are reclaimed viaFinalizationRegistry).require.js) is a pre-existing, environment-specific typelib conflict (PeasGtk / GIRepository 3.0 vs 2.0) that fails identically on unmodifiedmaster.🤖 Generated with Claude Code