From 409e403a35881e8548b9cba9775785f0fcb59798 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 11 Jun 2026 19:39:15 +0200 Subject: [PATCH] Fix compaction support When storing Ruby object that are movable in C global variable, the extension MUST declare these variables to the GC, otherwise they won't be marked nor updated when compaction occurs. --- ext/smarter_json/smarter_json.c | 7 +++++++ spec/spec_helper.rb | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/ext/smarter_json/smarter_json.c b/ext/smarter_json/smarter_json.c index 5852635..7bf91ed 100644 --- a/ext/smarter_json/smarter_json.c +++ b/ext/smarter_json/smarter_json.c @@ -1629,9 +1629,16 @@ static VALUE fj_parse_c(VALUE self, VALUE input, VALUE opts) { void Init_smarter_json(void) { mSmarterJSON = rb_define_module("SmarterJSON"); + + rb_global_variable(&cParseError); cParseError = rb_const_get(mSmarterJSON, rb_intern("ParseError")); + + rb_global_variable(&cEncodingError); cEncodingError = rb_const_get(mSmarterJSON, rb_intern("EncodingError")); + + rb_global_variable(&cWarning); cWarning = rb_const_get(mSmarterJSON, rb_intern("Warning")); + fj_bigdecimal_id = rb_intern("BigDecimal"); fj_to_sym_id = rb_intern("to_sym"); fj_key_p_id = rb_intern("key?"); diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6dcdb87..39c1508 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,16 @@ require "smarter_json" +if GC.respond_to?(:verify_compaction_references) + # This method was added in Ruby 3.0.0. Calling it this way asks the GC to + # move objects around, helping to find object movement bugs. + begin + GC.verify_compaction_references(expand_heap: true, toward: :empty) + rescue NotImplementedError, ArgumentError + # Some platforms don't support compaction + end +end + RSpec.configure do |config| # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status"