diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb index b7711e191dfb84..dbaf0aaf096920 100644 --- a/test/ruby/test_settracefunc.rb +++ b/test/ruby/test_settracefunc.rb @@ -625,6 +625,19 @@ def test_tracepoint_memory_leak CODE end + def test_tracepoint_bmethod_memory_leak + assert_no_memory_leak([], '', "#{<<~"begin;"}\n#{<<~'end;'}", "[Bug #20194]", rss: true) + obj = Object.new + obj.define_singleton_method(:foo) {} + bmethod = obj.method(:foo) + tp = TracePoint.new(:return) {} + begin; + 1_000_000.times do + tp.enable(target: bmethod) {} + end + end; + end + def trace_by_set_trace_func events = [] trace = nil diff --git a/vm_trace.c b/vm_trace.c index fb896fd423486a..9d0507879fe55a 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -1259,6 +1259,7 @@ rb_tracepoint_enable_for_target(VALUE tpval, VALUE target, VALUE target_line) (tp->events & (RUBY_EVENT_CALL | RUBY_EVENT_RETURN))) { if (def->body.bmethod.hooks == NULL) { def->body.bmethod.hooks = ZALLOC(rb_hook_list_t); + def->body.bmethod.hooks->is_local = true; } rb_hook_list_connect_tracepoint(target, def->body.bmethod.hooks, tpval, 0); rb_hash_aset(tp->local_target_set, target, Qfalse);