From: Yohanes Santoso Date: 2006-07-07T09:14:42+09:00 Subject: Re: GC oddness Alex Young writes: > nobu@ruby-lang.org wrote: >> Hi, >> At Thu, 6 Jul 2006 23:58:27 +0900, >> Alex Young wrote in [ruby-talk:200382]: >> >>> Can someone smarter tell me why this fails? I'm expecting the >>> TestDir to get GC'd when I call GC.start in the test, but >>> something's obviously keeping a reference lying around. I just >>> can't see what it is... >> The finalizer proc refers testdir. > I thought it might be that, but this also fails: It does not fail on me: require 'test/unit' require 'fileutils' class TestDir @@dir = nil @@clearup = proc {|id| FileUtils.rm_rf(@@dir); puts "Cleaned"} def TestDir.set_finalizer(testdir, dir) @@dir = dir ObjectSpace.define_finalizer(testdir, @@clearup) end def initialize(dir) FileUtils.mkdir(dir) TestDir.set_finalizer(self, dir) end end class DestroyTest < Test::Unit::TestCase def test_destroy a = TestDir.new('test_dir') a = nil GC.start puts "GC invoked" assert !File.directory?('test_dir') puts "Done" end end $ ruby ./gc.rb Loaded suite ./gc Started Cleaned GC invoked Done . Finished in 0.015623 seconds. 1 tests, 1 assertions, 0 failures, 0 errors $ ruby -v ruby 1.8.4 (2005-12-24) [i386-linux] Which OS and ruby are you using? YS.