From 6bad47ac6d62b54fe30e3f161c2a9d8f5fa4800c Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Wed, 5 Mar 2025 14:22:01 -0500 Subject: [PATCH] RUBY_FREE_AT_EXIT does not work when error in -r [Bug #21173] When loading a file using the command line -r, it is processed before RUBY_FREE_AT_EXIT is checked. So if the loaded file raises an error, it will cause memory to not be freed with RUBY_FREE_AT_EXIT. For example `ruby -rtest.rb -e ""` will report a large amount of memory leaks if `test.rb` raises. --- ruby.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ruby.c b/ruby.c index 059eb6652c..db60a67ecd 100644 --- a/ruby.c +++ b/ruby.c @@ -3131,6 +3131,11 @@ ruby_process_options(int argc, char **argv) ruby_init_setproctitle(argc, argv); #endif + if (getenv("RUBY_FREE_AT_EXIT")) { + rb_free_at_exit = true; + rb_category_warn(RB_WARN_CATEGORY_EXPERIMENTAL, "Free at exit is experimental and may be unstable"); + } + iseq = process_options(argc, argv, cmdline_options_init(&opt)); if (opt.crash_report && *opt.crash_report) { @@ -3138,11 +3143,6 @@ ruby_process_options(int argc, char **argv) ruby_set_crash_report(opt.crash_report); } - if (getenv("RUBY_FREE_AT_EXIT")) { - rb_free_at_exit = true; - rb_category_warn(RB_WARN_CATEGORY_EXPERIMENTAL, "Free at exit is experimental and may be unstable"); - } - return (void*)(struct RData*)iseq; }