On 17.01.2007 23:22, ara.t.howard / noaa.gov wrote:
> On Thu, 18 Jan 2007 paul.denize / datacom.co.nz wrote:
> 
>> I have Ruby 182-15 on WinXP
>>
>> When I run the code below only the first finalizer runs.  The others
>> two don't seem to run.  I found similar posts on other sites, but no
>> solutions.
>>
>> You can duplicate the first and it runs twice (so two finalizers is
>> ok).
>>
>> To me they are all somewhat identical.  Can anyone tell me why it does
>> not work.
> 
> finalizers cannot refer the to object being finalized.  in the second and
> third cases you hold a reference to the object (implicit in Proc.new/proc)
> inside the created closures - thereby preventing them from being run.
> inotherwords you've created finalizers that prevent the object from being
> finalized!  ;-)

Still the fact remains interesting that at least one of the other 
finalizers is being called when the process exits.  So the "blocking" 
finalizers just block themselves but not other finalizers.

10:35:25 [Temp]: ruby fin.rb
test4 136802980 2
test4 136802980 3
test3 136803090 1
test3 136803090 2
test2 136803180 1
test2 136803180 2
test1 136803250 1
test1 136803250 2
10:36:20 [Temp]: cat fin.rb

def test1
   o = Object.new
   ObjectSpace.define_finalizer(o) {|id| puts "test1 #{id} 1"}
   ObjectSpace.define_finalizer(o) {|id| puts "test1 #{id} 2"}
end

def test2
   o = Object.new
   ObjectSpace.define_finalizer(o) {|id| puts "test2 #{id} 1"}
   ObjectSpace.define_finalizer(o) {|id| puts "test2 #{id} 2"}
   ObjectSpace.define_finalizer(o) {|id| puts "test2 #{id} 3 #{o.inspect}"}
end

def test3
   o = Object.new
   ObjectSpace.define_finalizer(o) {|id| puts "test3 #{id} 1"}
   ObjectSpace.define_finalizer(o) {|id| puts "test3 #{id} 2"}

   o.instance_eval do
     ObjectSpace.define_finalizer(o) {|id| puts "test3 #{id} 3 #{inspect}"}
   end
end

def test4
   o = Object.new
   o.instance_eval do
     ObjectSpace.define_finalizer(o) {|id| puts "test4 #{id} 1 #{inspect}"}
   end

   ObjectSpace.define_finalizer(o) {|id| puts "test4 #{id} 2"}
   ObjectSpace.define_finalizer(o) {|id| puts "test4 #{id} 3"}
end

test1
test2
test3
test4
10:36:27 [Temp]:

Cheers

	robert