From: Rick DeNatale Date: 2008-01-11T09:19:36+09:00 Subject: Re: why does this code leak? This discussion reminds me of how such little details can have significant effects. Having the Proc#binding method seems to me to be somewhat similar to the "classical" Smalltalk dependency design. This was one of, if not the, first examples of the Observer pattern. Smalltalk defines methods on Object which allow dependents (observers) to be added to any object, an object notifies observers when it changes by self.changed which sends the message update to each dependent with the object as the parameter. Since this could be used with any object, but was actually used with few objects, the implementation in the Object class stored the list of dependents in a global identity dictionary(a hash which uses identity rather than equality in comparing keys) keyed on the object. What this means is that as long as an object has any dependents, it, and it's dependents can't be GCed, even though nothing outside of the dependency graph refers to any of those objects. For Smalltalk applications which actually used dependents it was common practice to override the method used to find the collection of dependents and keep it in an instance variable in the object itself rather than using the global identity dictionary. I just looked at the Squeak image and there's a subclass of Object called Model whose sole purpose is to do this. Interestingly, if one were to do this in Ruby, the default implementation could easily use an instance variable, since in Ruby unlike Smalltak, an instance variables don't take up any space in an object until it's actually needed. i.e. class Object def dependents # Defer actually creating a dependents iv until we have at least one dependent @dependents || [] end def add_dependent(dependent) (@dependents ||= []) << dependent end def changed self.dependents.each {|dependent| dependent.update(self)} end end This dynamic instance variable allocation is one of the reasons I now prefer Ruby to Smalltalk despite a long relationship with the former. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/