On 1/10/08, Tim Pease <tim.pease / gmail.com> wrote: > On Jan 10, 2008, at 5:26 AM, Robert Dober wrote: > > > class Foo > > def initialize > > ObjectSpace.define_finalizer self, lambda{} > > end > > end > When you create the lambda, what is the value of "self" inside the > lambda? > > The answer is that it is going to be the object in which the lambda > was created. In the code above, this would be the object that you are > trying to finalize -- i.e. an instance of Foo. Since the lambda has a > reference to the Foo instance, that instance will always be marked by > the GC, and hence, it will never be garbage collected. Right, this analysis is correct for Robert's code, and I was thinking the same thing about Ara's "leaky_finalizer" code as well, but that code, here simplified, doesn't have the same problem as far as I can tell: class Class def leaky_finalizer lambda{} end def new *a, &b object = allocate object.send :initialize, *a, &b object ensure ObjectSpace.define_finalizer object, leaky_finalizer end end end Note that we are in class Class so self when the lambda is created is not the new instance but Class itself. In this case it looks as if the lambda (or something else) is holding on to the binding of the caller of the finalize method where object is bound to the object to be finalized. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/