fix(examples): don't unref the MainLoop in glib-timeout (#429)#431
Merged
Conversation
The example followed the C idiom of balancing g_main_loop_new() with g_main_loop_unref(), but node-gtk's boxed wrapper already owns that reference and releases it on GC. The explicit loop.unref() drops it a second time, and the resulting double-free of the GMainLoop corrupts the heap — which, with gi.startLoop() active, faults during node's loop teardown at process exit (SIGSEGV in uv_close). Verified on Ubuntu 26 (libffi 3.5): 30/30 runs crash with the unref, 0/30 without. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jun 14, 2026
Closed
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.
Summary
Fixes the crash in
node examples/glib-timeout.jstracked by #429.The example balanced
new GLib.MainLoop()withloop.unref()(the C idiom). But node-gtk's boxed wrapper already owns that reference andg_main_loop_unref()s it on GC, so the explicitloop.unref()drops it a second time → double-free of theGMainLoop. Standalone that's only ag_main_loop_unref: ref_count > 0warning, but withgi.startLoop()active the heap corruption faults during Node's loop teardown at exit (SIGSEGV inuv_close).This removes the
loop.unref()(and fixes the stalecallback.jsheader).Verified on Ubuntu 26.04 (rootless podman; libffi 3.5.2 / gir 1.86.0 / glib 2.88 / node 22)
loop.unref())Full run is clean:
Run loop. → count 0..3 → Loop ran., exit 0.Note (separate, not in this PR)
This is a narrow fix for the example. The underlying hazard — node-gtk exposing C
ref()/unref()/free()on GC-owned boxed wrappers, so following normal C ownership idioms double-frees — is left as an open design question in #429.🤖 Generated with Claude Code