diff options
author | Peter Zhu <[email protected]> | 2024-01-16 15:22:03 -0500 |
---|---|---|
committer | Peter Zhu <[email protected]> | 2024-01-16 16:19:43 -0500 |
commit | 5471f99eead4dd77f1d418eca04d7656bb97f01d (patch) | |
tree | 0a14aa98b404dd64ae0082d06a15bbf64240f558 /iseq.c | |
parent | 7b6731b1bb7c8fab72580f92450eea6e4cc3d943 (diff) |
[PRISM] Fix memory leak when compiling file
There is a memory leak when passing a file to
RubyVM::InstructionSequence.compile_prism because it does not free the
mapped file.
For example:
require "tempfile"
Tempfile.create(%w"test_iseq .rb") do |f|
f.puts "name = 'Prism'; puts 'hello'"
f.close
10.times do
1_000.times do
RubyVM::InstructionSequence.compile_prism(f)
end
puts `ps -o rss= -p #{$$}`
end
end
Before:
27968
44848
61408
77872
94144
110432
126640
142816
159200
175584
After:
11504
12144
12592
13072
13488
13664
14064
14368
14704
15168
Diffstat (limited to 'iseq.c')
-rw-r--r-- | iseq.c | 11 |
1 files changed, 7 insertions, 4 deletions
@@ -1477,23 +1477,26 @@ iseqw_s_compile_prism(int argc, VALUE *argv, VALUE self) pm_parser_t parser; + pm_string_t input; if (RB_TYPE_P(src, T_FILE)) { FilePathValue(src); file = rb_fstring(src); /* rb_io_t->pathv gets frozen anyways */ - pm_string_t input; pm_string_mapped_init(&input, RSTRING_PTR(file)); - - pm_parser_init(&parser, pm_string_source(&input), pm_string_length(&input), &options); } else { - pm_parser_init(&parser, (const uint8_t *) RSTRING_PTR(src), RSTRING_LEN(src), &options); + input.source = (const uint8_t *)RSTRING_PTR(src); + input.length = RSTRING_LEN(src); + input.type = PM_STRING_SHARED; } + pm_parser_init(&parser, pm_string_source(&input), pm_string_length(&input), &options); + rb_iseq_t *iseq = iseq_alloc(); iseqw_s_compile_prism_compile(&parser, opt, iseq, file, path, start_line); pm_parser_free(&parser); pm_options_free(&options); + pm_string_free(&input); return iseqw_new(iseq); } |