Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c082a98

Browse files
committed
rb_gc_impl_copy_finalizer: generate a new object id
Fix a regression introduced by: #13155
1 parent a707f06 commit c082a98

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

gc/default/default.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2793,7 +2793,8 @@ rb_gc_impl_copy_finalizer(void *objspace_ptr, VALUE dest, VALUE obj)
27932793

27942794
int lev = rb_gc_vm_lock();
27952795
if (RB_LIKELY(st_lookup(finalizer_table, obj, &data))) {
2796-
table = (VALUE)data;
2796+
table = rb_ary_dup((VALUE)data);
2797+
RARRAY_ASET(table, 0, rb_obj_id(dest));
27972798
st_insert(finalizer_table, dest, table);
27982799
FL_SET(dest, FL_FINALIZE);
27992800
}

gc/mmtk/mmtk.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,8 @@ rb_gc_impl_copy_finalizer(void *objspace_ptr, VALUE dest, VALUE obj)
985985

986986
int lev = rb_gc_vm_lock();
987987
if (RB_LIKELY(st_lookup(objspace->finalizer_table, obj, &data))) {
988-
table = (VALUE)data;
988+
table = rb_ary_dup((VALUE)data);
989+
RARRAY_ASET(table, 0, rb_obj_id(dest));
989990
st_insert(objspace->finalizer_table, dest, table);
990991
FL_SET(dest, FL_FINALIZE);
991992
}

test/ruby/test_objectspace.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_count_objects
9494
end
9595

9696
def test_finalizer
97-
assert_in_out_err(["-e", <<-END], "", %w(:ok :ok :ok :ok), [])
97+
assert_in_out_err(["-e", <<-END], "", %w(:ok :ok :ok), [])
9898
a = []
9999
ObjectSpace.define_finalizer(a) { p :ok }
100100
b = a.dup
@@ -137,6 +137,25 @@ class << fin
137137
}
138138
end
139139

140+
def test_finalizer_copy
141+
assert_in_out_err(["-e", <<~'RUBY'], "", %w(:ok), [])
142+
def fin
143+
ids = Set.new
144+
->(id) { puts "object_id (#{id}) reused" unless ids.add?(id) }
145+
end
146+
147+
OBJ = Object.new
148+
ObjectSpace.define_finalizer(OBJ, fin)
149+
OBJ.freeze
150+
151+
10.times do
152+
OBJ.clone
153+
end
154+
155+
p :ok
156+
RUBY
157+
end
158+
140159
def test_finalizer_with_super
141160
assert_in_out_err(["-e", <<-END], "", %w(:ok), [])
142161
class A

0 commit comments

Comments
 (0)