From: Yohanes Santoso Date: 2005-04-13T15:09:02+09:00 Subject: Re: ability to run finalizers at a given point of a program? Guillaume Cottenceau writes: > What version of Ruby are you running? With your example I can see: > > *** constructor > Foo was out-scoped. > Gc was run. > *** pseudo-destructor $ ruby -v ruby 1.8.2 (2004-12-06) [i386-linux] # Guillaume Cottenceau's version $ ruby /tmp/guil.rb *** constructor Foo was out-scoped. Gc was run. *** pseudo-destructor # TS' version $ ruby /tmp/ts.rb *** constructor Foo was out-scoped. *** pseudo-destructor Gc was run. So, I am getting the same result ts was getting. > What difference do you pretend your program is making with mine? The difference is in this: (your version): def scopeme foo = Foo.new ObjectSpace.define_finalizer(foo, proc { puts "*** pseudo-destructor" }) end That block captures foo as well. As a result, that instance will never be unreferenced, and thus never going to be GC'ed until the program ends. OTOH, ts' version: class Foo def self.final proc { puts "*** pseudo-destructor" } end end def scopeme foo = Foo.new ObjectSpace.define_finalizer(foo, Foo.final) end Notice that the finalizer block does not capture the Foo instance. YS.