Skip to content
This repository
Browse code

Fixed memory leak with Object#remove_subclasses_of, which inflicted a…

… Rails application running in development mode with a ~20KB leak per request #1289 [c.r.mcgrath@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1569 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 7e1d002673777bb1240e27949a853a8f287d1f56 1 parent a90598b
David Heinemeier Hansson authored
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Fixed memory leak with Object#remove_subclasses_of, which inflicted a Rails application running in development mode with a ~20KB leak per request #1289 [c.r.mcgrath@gmail.com]
  4
+
3 5
 * Made 1.year == 365.25.days to account for leap years.  This allows you to do User.find(:all, :conditions => ['birthday > ?', 50.years.ago]) without losing a lot of days.  #1488 [tuxie@dekadance.se]
4 6
 
5 7
 * Added an exception if calling id on nil to WhinyNil #584 [kevin-temp@writesoon.com]
9  activesupport/lib/active_support/core_ext/object_and_class.rb
... ...
@@ -1,6 +1,13 @@
1 1
 class Object #:nodoc:
2 2
   def remove_subclasses_of(superclass)
3  
-    subclasses_of(superclass).each { |subclass| Object.send(:remove_const, subclass) rescue nil }
  3
+    subclasses_of(superclass).each do |subclass|
  4
+      ObjectSpace.each_object(Class) do |k|
  5
+        if k.to_s == subclass
  6
+          k.instance_variables.each { |v| k.send(:remove_instance_variable, v) }
  7
+        end  
  8
+      end   
  9
+      Object.send(:remove_const, subclass) rescue nil
  10
+    end 
4 11
   end
5 12
 
6 13
   def subclasses_of(superclass)
18  activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -5,6 +5,13 @@ class ClassA; end
5 5
 class ClassB < ClassA; end
6 6
 class ClassC < ClassB; end
7 7
 class ClassD < ClassA; end
  8
+class RemoveSubsTestClass; end
  9
+class RemoveSubsBaseClass
  10
+  def self.add_ivar
  11
+    @ivar = RemoveSubsTestClass.new
  12
+  end
  13
+end
  14
+class RemoveSubsSubClass < RemoveSubsBaseClass; end
8 15
 
9 16
 class ClassExtTest < Test::Unit::TestCase
10 17
   def test_methods
@@ -30,4 +37,15 @@ def test_suppress_supresses
30 37
     suppress(LoadError, ArgumentError) { raise LoadError }
31 38
     suppress(LoadError, ArgumentError) { raise ArgumentError }
32 39
   end
  40
+  
  41
+  def test_remove_subclasses_of_unsets_ivars
  42
+    r = RemoveSubsSubClass.new
  43
+    RemoveSubsSubClass.add_ivar
  44
+    RemoveSubsBaseClass.remove_subclasses
  45
+
  46
+    GC.start
  47
+    ObjectSpace.each_object do |o|
  48
+      flunk("ObjectSpace still contains RemoveSubsTestClass") if o.class == RemoveSubsTestClass
  49
+    end
  50
+  end
33 51
 end

0 notes on commit 7e1d002

Please sign in to comment.
Something went wrong with that request. Please try again.