Project

General

Profile

Actions

Bug #18516

closed

Memory leak on aliasing method to itself

Added by ibylich (Ilya Bylich) about 2 years ago. Updated almost 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.0dev
[ruby-core:107287]

Description

The following code produces a memory leak:

class A
  1.upto(Float::INFINITY) do |i|
    define_method(:"foo_#{i}") {}

    alias :"foo_#{i}" :"foo_#{i}"

    remove_method :"foo_#{i}"
  end
end

It is very artificial, but it's required for LSAN/Valgrind integration.

The reason why it leaks is the following:

  • alias foo foo increments alias_count even if no alias is created
  • later during GC rb_free_method_entry is called that in turn calls rb_method_definition_release that calls free only if alias_count + complemented_count == 0.
  • In this particular case alias_count is 1, and so no cleanup happens.

I've been asked to create tickets for memory leaks that I find during LSAN integration if the leak affects previous versions of Ruby. From what I see it does.

Possible fix: https://github.com/ruby/ruby/pull/5492. But like I mentioned in PR I'm not sure if it's correct, @nobu (Nobuyoshi Nakada) mentioned in my main PR aliasing method to itself is used to swallow some warning on method redefinition, but I don't what's the case there.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0