fix: disown boxed wrappers freed via *_free/*_unref methods (#429)#432
Merged
Conversation
A boxed/struct wrapper owns its memory and frees it on GC. When user code calls an introspected method that frees the instance itself (e.g. GLib.MainLoop's unref(), following the C idiom), node-gtk still believed it owned the memory and freed it again at GC — a double-free. Benign on lenient allocators, but a hard SIGSEGV during node's loop teardown with gi.startLoop() on libffi 3.5 (Ubuntu 26). After a *_free/*_unref instance method on a struct/union/boxed, disown the wrapper: clear owns_memory so the finalizer won't free it, and null the data pointer. Worst case becomes a leak (e.g. unref() that didn't drop the last ref) rather than a crash; mixing manual refcounting with node-gtk's GC ownership was already unsupported. Co-Authored-By: Claude Opus 4.8 (1M context) <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.
Summary
Core fix for #429. A boxed/struct wrapper owns its backing memory and frees it on GC (gated by
Boxed::owns_memory). When user code calls an introspected method that frees the instance itself — e.g.GLib.MainLoop'sunref(), following the normal C idiom — node-gtk still thought it owned the memory and freed it again at GC. That double-free is a benigng_main_loop_unref: ref_count > 0warning on lenient allocators, but a hard SIGSEGV during node's loop teardown whengi.startLoop()is active on libffi 3.5 (Ubuntu 26).This is the general counterpart to the example fix in #431.
Fix
After an instance method whose C symbol ends in
_free/_unrefon a struct/union/boxed container, disown the wrapper (src/function.cc→ newDisownBoxedinsrc/boxed.cc):owns_memoryso the GC finalizer won't free it again;Scoped deliberately:
gtk_widget_destroy()& co. don't free the wrapper (hence_free/_unrefonly, not_destroy).unref()s a multi-ref object without dropping the last ref, the wrapper simply won't unref at GC. Mixing manual refcounting with node-gtk's GC ownership was already unsupported.Verified on Ubuntu 26.04 (rootless podman; libffi 3.5.2 / gir 1.86.0 / glib 2.88 / node 22)
The crashing pattern —
gi.startLoop()+new GLib.MainLoop()+ explicitloop.unref()+ natural exit:Plus a new regression test
tests/conversion__boxed_explicit_unref.js(passes), and the full suite stays green locally (76 passing; only the pre-existing envrequire.js).Note
Leaves the broader question (should
ref/unref/freebe hidden entirely?) untouched — this makes the common, idiomatic case safe with a minimal, scoped change.🤖 Generated with Claude Code