diff options
author | shirosaki <shirosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-24 12:26:54 +0000 |
---|---|---|
committer | shirosaki <shirosaki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-24 12:26:54 +0000 |
commit | 1cdeab5cdd4b84cc0d97ee1a148beeb11bdd2d44 (patch) | |
tree | db35f8ee395b34c1b1a8b8a82a13afb8c4fd2ef3 /gc.c | |
parent | e6c2ffa981f00c249a9a6a1595ecab18d0a6f741 (diff) | |
download | ruby-1cdeab5cdd4b84cc0d97ee1a148beeb11bdd2d44.tar.gz |
Fix WeakRef finalize
* array.c (rb_ary_delete_same_obj): new function for WeakRef.
This deletes same objects as item argument in the array.
* internal.h (rb_ary_delete_same_obj): add a declaration.
* gc.c (wmap_final_func): remove WeakRef object reference from the
array. rb_ary_delete() is not usable because it uses rb_equal() to
compare object references.
* gc.c (wmap_finalize): remove recycled object references from weak
map hash properly. How to get object reference from object id was
wrong. st_delete() doesn't work properly if key and value arguments
are same. The key of obj2wmap is referenced object and the value of
obj2wmap is WeakRef array.
* gc.c (wmap_aset): obj2wmap should contain WeakRef array in the
definition.
* test/test_weakref.rb
(TestWeakRef#test_not_reference_different_object,
TestWeakRef#test_weakref_finalize): add tests for above.
[ruby-core:49044] [Bug #7304]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37834 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'gc.c')
-rw-r--r-- | gc.c | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -3751,8 +3751,8 @@ wmap_final_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing) { VALUE obj, ary; if (!existing) return ST_STOP; - obj = (VALUE)*key, ary = (VALUE)*value; - rb_ary_delete(ary, obj); + obj = (VALUE)arg, ary = (VALUE)*value; + rb_ary_delete_same_obj(ary, obj); if (!RARRAY_LEN(ary)) return ST_DELETE; return ST_CONTINUE; } @@ -3760,16 +3760,18 @@ wmap_final_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing) static VALUE wmap_finalize(VALUE self, VALUE obj) { - st_data_t data; + st_data_t key, data; VALUE rids; long i; struct weakmap *w; TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w); - obj = NUM2PTR(obj); + /* Get reference from object id. */ + obj = obj ^ 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. */ + key = (st_data_t)obj; + if (st_delete(w->obj2wmap, &key, &data)) { rids = (VALUE)data; for (i = 0; i < RARRAY_LEN(rids); ++i) { data = (st_data_t)RARRAY_PTR(rids)[i]; @@ -3777,9 +3779,9 @@ wmap_finalize(VALUE self, VALUE obj) } } - data = (st_data_t)obj; - if (st_delete(w->wmap2obj, &data, &data)) { - st_update(w->obj2wmap, (st_data_t)obj, wmap_final_func, 0); + key = (st_data_t)obj; + if (st_delete(w->wmap2obj, &key, &data)) { + st_update(w->obj2wmap, data, wmap_final_func, (st_data_t)obj); } return self; } @@ -3801,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); } |