From: Alex Young Date: 2006-07-07T00:46:50+09:00 Subject: Re: GC oddness 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: ----------------------------- require 'test/unit' require 'fileutils' class TestDir @@dir = nil @@clearup = proc {|id| FileUtils.rm_rf(@@dir)} 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 assert !File.directory?('test_dir') end end ---------------------------- testdir hasn't come into existence when the proc's created this time, so I don't see how it can be keeping a reference to it, or how a finalizer proc keeping a reference to the object that's being finalized can stop GC - if that's the case, how does the object ever get GC'd? What have I missed? -- Alex