Skip to content

Commit

Permalink
Fixed memory leak with Object#remove_subclasses_of, which inflicted a…
Browse files Browse the repository at this point in the history
… 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
dhh committed Jun 30, 2005
1 parent a90598b commit 7e1d002
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* 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]

* 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] * 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]


* Added an exception if calling id on nil to WhinyNil #584 [kevin-temp@writesoon.com] * Added an exception if calling id on nil to WhinyNil #584 [kevin-temp@writesoon.com]
Expand Down
@@ -1,6 +1,13 @@
class Object #:nodoc: class Object #:nodoc:
def remove_subclasses_of(superclass) def remove_subclasses_of(superclass)
subclasses_of(superclass).each { |subclass| Object.send(:remove_const, subclass) rescue nil } subclasses_of(superclass).each do |subclass|
ObjectSpace.each_object(Class) do |k|
if k.to_s == subclass
k.instance_variables.each { |v| k.send(:remove_instance_variable, v) }
end
end
Object.send(:remove_const, subclass) rescue nil
end
end end


def subclasses_of(superclass) def subclasses_of(superclass)
Expand Down
18 changes: 18 additions & 0 deletions activesupport/test/core_ext/object_and_class_ext_test.rb
Expand Up @@ -5,6 +5,13 @@ class ClassA; end
class ClassB < ClassA; end class ClassB < ClassA; end
class ClassC < ClassB; end class ClassC < ClassB; end
class ClassD < ClassA; end class ClassD < ClassA; end
class RemoveSubsTestClass; end
class RemoveSubsBaseClass
def self.add_ivar
@ivar = RemoveSubsTestClass.new
end
end
class RemoveSubsSubClass < RemoveSubsBaseClass; end


class ClassExtTest < Test::Unit::TestCase class ClassExtTest < Test::Unit::TestCase
def test_methods def test_methods
Expand All @@ -30,4 +37,15 @@ def test_suppress_supresses
suppress(LoadError, ArgumentError) { raise LoadError } suppress(LoadError, ArgumentError) { raise LoadError }
suppress(LoadError, ArgumentError) { raise ArgumentError } suppress(LoadError, ArgumentError) { raise ArgumentError }
end end

def test_remove_subclasses_of_unsets_ivars
r = RemoveSubsSubClass.new
RemoveSubsSubClass.add_ivar
RemoveSubsBaseClass.remove_subclasses

GC.start
ObjectSpace.each_object do |o|
flunk("ObjectSpace still contains RemoveSubsTestClass") if o.class == RemoveSubsTestClass
end
end
end end

0 comments on commit 7e1d002

Please sign in to comment.