From c6071df4582be8adcb85800aa995a03978b915f0 Mon Sep 17 00:00:00 2001 From: Hiroshi Shirosaki Date: Fri, 9 Nov 2012 11:35:29 +0900 Subject: [PATCH] Fix WeakRef finalize * gc.c (wmap_final_func): remove WeakRef objects from array. * gc.c (wmap_finalize): remove recycled objects from weakmap hash. * gc.c (wmap_aset): obj2wmap should have WeakRef array. * test/test_weakref.rb (TestWeakRef#test_not_reference_different_object): add a test for above fix. [ruby-core:49044] [Bug #7304] --- gc.c | 33 ++++++++++++++++++--------------- test/test_weakref.rb | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/gc.c b/gc.c index b930621..e8d3648 100644 --- a/gc.c +++ b/gc.c @@ -3748,37 +3748,40 @@ wmap_allocate(VALUE klass) static int wmap_final_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing) { - VALUE obj, ary; + VALUE wmap, ary; if (!existing) return ST_STOP; - obj = (VALUE)*key, ary = (VALUE)*value; - rb_ary_delete(ary, obj); + wmap = (VALUE)arg, ary = (VALUE)*value; + rb_ary_delete(ary, wmap); if (!RARRAY_LEN(ary)) return ST_DELETE; return ST_CONTINUE; } static VALUE -wmap_finalize(VALUE self, VALUE obj) +wmap_finalize(VALUE self, VALUE objid) { - st_data_t data; - VALUE rids; + st_data_t orig, wmap, data; + VALUE obj, rids; long i; struct weakmap *w; TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w); - obj = NUM2PTR(obj); + /* Get reference from object id. */ + obj = objid ^ FIXNUM_FLAG; /* unset FIXNUM_FLAG */ - data = (st_data_t)obj; - if (st_delete(w->obj2wmap, &data, &data)) { + /* obj is original referenced object and/or weak reference. */ + orig = (st_data_t)obj; + if (st_delete(w->obj2wmap, &orig, &data)) { rids = (VALUE)data; for (i = 0; i < RARRAY_LEN(rids); ++i) { - data = (st_data_t)RARRAY_PTR(rids)[i]; - st_delete(w->wmap2obj, &data, NULL); + wmap = (st_data_t)RARRAY_PTR(rids)[i]; + st_delete(w->wmap2obj, &wmap, NULL); } } - data = (st_data_t)obj; - if (st_delete(w->wmap2obj, &data, &data)) { - st_update(w->obj2wmap, (st_data_t)obj, wmap_final_func, 0); + wmap = (st_data_t)obj; + if (st_delete(w->wmap2obj, &wmap, &orig)) { + wmap = (st_data_t)obj; + st_update(w->obj2wmap, orig, wmap_final_func, wmap); } return self; } @@ -3800,7 +3803,7 @@ wmap_aset(VALUE self, VALUE wmap, VALUE orig) rids = rb_ary_tmp_new(1); st_insert(w->obj2wmap, (st_data_t)orig, (st_data_t)rids); } - rb_ary_push(rids, orig); + rb_ary_push(rids, wmap); st_insert(w->wmap2obj, (st_data_t)wmap, (st_data_t)orig); return nonspecial_obj_id(orig); } diff --git a/test/test_weakref.rb b/test/test_weakref.rb index 0f943cd..6c80f59 100644 --- a/test/test_weakref.rb +++ b/test/test_weakref.rb @@ -21,4 +21,23 @@ class TestWeakRef < Test::Unit::TestCase ObjectSpace.garbage_collect assert_raise(WeakRef::RefError) {weak.to_s} end + + def test_not_reference_different_object + bug7304 = '[ruby-core:49044]' + weakrefs = [] + 3.times do + obj = Object.new + def obj.foo; end + weakrefs << WeakRef.new(obj) + ObjectSpace.garbage_collect + end + assert_nothing_raised(NoMethodError, bug7304) { + weakrefs.each do |weak| + begin + weak.foo + rescue WeakRef::RefError + end + end + } + end end -- 1.7.9.1