Joshua Chia wrote:
> The finalizer is not getting called in this code -- it just prints
> 'exiting'.  What's wrong?  I'm running it on v1.8.6 on Windows Vista.
> 
> class A
>   def initialize
>      ObjectSpace.define_finalizer(self, proc{|id| puts "finalizing
> #{id}"})
>   end
> end
> a = A.new
> a = nil
> GC.start
> sleep(5)
> puts 'exiting'

I used this example with Windows2000 and the same result. It works when 
I put the finalizer definition outside of the class definition.

>>>>> Code >>>>>
class A
  def initialize
    puts "initializing #{self.object_id}"
#    ObjectSpace.define_finalizer(self, proc{|id| puts "finalizing 
#{id}"})
  end
end
a = A.new
puts "created #{a.object_id}"
ObjectSpace.define_finalizer(a, proc{|id| puts "finalizing #{id}"})
a = nil
GC.start
puts 'exiting'
>>>>> Output >>>>>
initializing 24036900
created 24036900
exiting
finalizing 24036900
>>>>> EOE >>>>>

When I change your original code a little bit, there will be an 
interesting result...

>>>>> Code >>>>>
class A
  def initialize
    puts "initializing #{self.object_id}"
    ObjectSpace.define_finalizer(self, proc{|id| puts "finalizing 
#{id}"})
  end
end
a = A.new
puts "created #{a.object_id}"
#ObjectSpace.define_finalizer(a, proc{|id| puts "finalizing #{id}"})
a = nil
GC.start
ObjectSpace.each_object(A){|o|puts "still there: #{o.object_id}"}
puts 'exiting'
>>>>> Output >>>>>
initializing 24036690
created 24036690
still there: 24036690
exiting
>>>>> EOE >>>>>

...which means, that the object was not destroyed by the GC. I don't see 
any additional actual reference to the object and don't understand this.

Wolfgang NĂ¡dasi-Donner
-- 
Posted via http://www.ruby-forum.com/.