From 1c2c9dd01b300cda0766467deed84f8143c40272 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Wed, 23 Aug 2023 10:05:24 -0400 Subject: [PATCH] Remove @_memoized ivar from Minitest::Spec objects We need to remove the @_memoized instance variable from Minitest::Spec objects because it holds a hash that contains memoized objects in `let` blocks, this can contain objects that will be reported as a memory leak. --- lib/ruby_memcheck/test_helper.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/ruby_memcheck/test_helper.rb b/lib/ruby_memcheck/test_helper.rb index d9cf9e5..cf2534c 100644 --- a/lib/ruby_memcheck/test_helper.rb +++ b/lib/ruby_memcheck/test_helper.rb @@ -5,5 +5,18 @@ f.write($LOADED_FEATURES.join("\n")) end + # We need to remove the @_memoized instance variable from Minitest::Spec + # objects because it holds a hash that contains memoized objects in `let` + # blocks, this can contain objects that will be reported as a memory leak. + if defined?(Minitest::Spec) + require "objspace" + + ObjectSpace.each_object(Minitest::Spec) do |obj| + if obj.instance_variable_defined?(:@_memoized) + obj.remove_instance_variable(:@_memoized) + end + end + end + GC.start end