aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--gc.c14
-rw-r--r--test/test_weakref.rb8
3 files changed, 28 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index ddc6216e62..2149f07021 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Sat Dec 13 09:58:41 2014 Eric Wong <e@80x24.org>
+
+ * gc.c (define_final0): avoid duplicate blocks
+ [Bug #10537]
+ * test/test_weakref.rb (test_repeated_object_leak): new test
+
Sat Dec 13 04:59:20 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* bin/erb (ERB::Main#run): get rid of shadowing outer local
diff --git a/gc.c b/gc.c
index feb9c08059..1caa790202 100644
--- a/gc.c
+++ b/gc.c
@@ -2370,6 +2370,20 @@ define_final0(VALUE obj, VALUE block)
if (st_lookup(finalizer_table, obj, &data)) {
table = (VALUE)data;
+
+ /* avoid duplicate block, table is usually small */
+ {
+ const VALUE *ptr = RARRAY_CONST_PTR(table);
+ long len = RARRAY_LEN(table);
+ long i;
+
+ for (i = 0; i < len; i++, ptr++) {
+ if (rb_funcall(*ptr, idEq, 1, block)) {
+ return *ptr;
+ }
+ }
+ }
+
rb_ary_push(table, block);
}
else {
diff --git a/test/test_weakref.rb b/test/test_weakref.rb
index 129c1e6ed1..36b5d5bc51 100644
--- a/test/test_weakref.rb
+++ b/test/test_weakref.rb
@@ -60,4 +60,12 @@ class TestWeakRef < Test::Unit::TestCase
end
}, bug7304
end
+
+ def test_repeated_object_leak
+ bug10537 = '[ruby-core:66428]'
+ assert_no_memory_leak(%w(-rweakref), '', <<-'end;', bug10537)
+ a = Object.new
+ 150_000.times { WeakRef.new(a) }
+ end;
+ end
end