From: ara howard Date: 2008-01-11T01:14:52+09:00 Subject: Re: why does this code leak? On Jan 10, 2008, at 5:26 AM, Robert Dober wrote: > Just trying to answer the question if it is a bug by making a minimum > version of the leaking version (and correcting Ara's terrible bug how > to write 7 ;) and running it with 1.8 and 1.9 > 591/92 > cat leak.rb > # vim: sw=2 ts=2 ft=ruby expandtab tw=0 nu syn: > # > class Foo > def initialize > ObjectSpace.define_finalizer self, lambda{} > end > end > > (42/6).times { > GC.start nice! ;-) so i posted something like this over on ruby-core, which i'll add for posterity: " to add a note to the end of the thread the fix to my problem was essentially this class Class New = instance_method :new Objects = Hash.new Destroy = lambda{|object_id| Objects.delete object_id} def new *a, &b object = allocate Objects[object.object_id] = caller object.send :initialize *a, &b object ensure ObjectSpace.define_finalizer object, Destroy end end and that class Class def destroy lambda{} end .... ObjectSpace.define_finalizer object, destroy .... end perhaps i have not explained this adequately, but i still feel this is a bug. the 'self' that is enclosed is *never* 'object' and that self has no reference, save a local variable in another function, that refers to 'object'. in any case i have a workaround and dike.rb is better than ever (see [ANN] on ruby-talk) so thanks all for the input! " a @ http://codeforpeople.com/ -- share your knowledge. it's a way to achieve immortality. h.h. the 14th dalai lama