From: "Wolfgang NĂ¡dasi-donner" Date: 2007-08-03T02:50:37+09:00 Subject: Re: finalizer not getting called Joshua Chia wrote: > I'm not familiar with all the nuances for blocks, but I only see that > the block makes no reference to self. Why does it need to hold on to a > reference to self? Blocks act as closures, which means, that variables not defined as block parameter but used inside the block, will be taken from the environment in which the block was defined. In this case the value of "self" - which refers the new defined object - must be saved for this purpose. If you define the block outside of initialize, it will work. >>>>> Code >>>>> class A def initialize(b) puts "initializing #{self.object_id}" ObjectSpace.define_finalizer(self, b) end end a = A.new(proc{|id| puts "finalizing #{id}"}) puts "created #{a.object_id}" a = nil GC.start ObjectSpace.each_object(A){|o|puts "still there: #{o.object_id}"} puts 'exiting' >>>>> Result >>>>> initializing 24036640 created 24036640 finalizing 24036640 exiting >>>>> EOE >>>>> -- Posted via http://www.ruby-forum.com/.