Project

General

Profile

Actions

Bug #19894

closed

Memory leak in complemented method entries

Added by peterzhu2118 (Peter Zhu) 8 months ago. Updated 7 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:114826]

Description

GitHub pull request: https://github.com/ruby/ruby/pull/8481

When a copy of a complemented method entry is created, there are two issues:

  1. IMEMO_FL_USER3 is not copied, so the complemented status is not copied over.
  2. In rb_method_entry_clone we increment both alias_count and complemented_count. However, when we free the method entry in rb_method_definition_release, we only decrement one of the two counters, resulting in the rb_method_definition_t being leaked.

The following script reproduces this issue:

10.times do
  20_000.times do
    $c = Class.new do
      def foo; end
    end

    $m = Module.new do
      refine $c do
        def foo; end
      end
    end

    Class.new do
      using $m

      def initialize
        o = $c.new
        o.method(:foo).unbind
      end
    end.new
  end

  puts `ps -o rss= -p #{$$}`
end

Before this fix:

 17328
 19248
 21408
 23296
 25600
 27408
 29424
 31520
 33728
 35664

After this fix:

 16240
 17680
 18800
 19744
 20640
 21840
 22896
 24336
 25280
 26096
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0