Skip to content

Commit 07b59ee

Browse files
committed
Fix memory leak when load_from_binary raises
ibf_load_code will leak memory allocated for the code if an exception is raised. The following script reproduces the leak: bin = RubyVM::InstructionSequence.of(1.method(:abs)).to_binary 10.times do 100_000.times do RubyVM::InstructionSequence.load_from_binary(bin) rescue ArgumentError end puts `ps -o rss= -p #{$$}` end Before: 18004 23380 28756 34260 39892 45396 50772 55892 61012 66132 After: 12536 12920 13304 13688 14072 14456 14840 15352 15608 15864
1 parent d0d1246 commit 07b59ee

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

compile.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12926,6 +12926,9 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod
1292612926
struct rb_call_data *cd_entries = load_body->call_data;
1292712927
int ic_index = 0;
1292812928

12929+
load_body->iseq_encoded = code;
12930+
load_body->iseq_size = 0;
12931+
1292912932
iseq_bits_t * mark_offset_bits;
1293012933

1293112934
iseq_bits_t tmp[1] = {0};
@@ -13057,7 +13060,6 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod
1305713060
}
1305813061
}
1305913062

13060-
load_body->iseq_encoded = code;
1306113063
load_body->iseq_size = code_index;
1306213064

1306313065
if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {

0 commit comments

Comments
 (0)