Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,10 @@ mrb_ary_delete_at(mrb_state *mrb, mrb_value self)
ary_modify(mrb, a);
mrb_value *ptr = ARY_PTR(a);
mrb_value val = ptr[index];
/* the shrink below may collect, and val is then out of the array:
protect it while the array still holds it, since growing the arena
may collect too */
mrb_gc_protect(mrb, val);

ptr += index;
mrb_int len = alen - index;
Expand Down
6 changes: 6 additions & 0 deletions test/t/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ def []=(*args)
assert_equal([1], a)
end

assert('Array#delete_at keeps what it returns while the array shrinks') do
# the strings are held by the array alone, and its capacity shrinks
a = (1..40).map { |i| "s#{i}" }
35.times { |i| assert_equal("s#{i + 1}", a.delete_at(0)) }
end

assert('Array#each', '15.2.12.5.10') do
a = [1,2,3]
b = 0
Expand Down
Loading