From: Nat Pryce Date: 2001-10-24T20:42:13+09:00 Subject: [ruby-talk:23180] Finalisers (was Re: Re: Bruce Eckel's opinion of Ruby) ----- Original Message ----- From: "Avdi B. Grimm" > On Tue, 2001-10-23 at 19:27, Michael Neumann wrote: > > True. You cannot do everything with blocks. > > In Ruby you have class ObjectSpace that lets you define finalizer > > methods. This is not very nice, but it works. > I would go so far as to say it doesn't work at all, unless I completely > failed to understand it. Once the ObjectSpace-defined constructor kicks > in, the object is *already gone*. All the destructor gets is (IIRC) the > ID of the object. I haven't yet figured out how this can be used to > free resources that only the now-deceased object posessed the keys to. > Perhaps one has to maintain some sort of global ID/Resource map, and the > destructor has to look up the resources to free via the ID...? Pardon > me but, yeeeecchhhhh! That's just horrid. The finaliser block is a *closure* and so can hold references to any resources owned by the GC'd object that need to be cleaned up. Just be careful that the closure doesn't hold a reference to the object itself, or it will never be collected. This has been discussed before on this list, and has been summarised on the Ruby Garden wiki at http://www.rubygarden.org/ruby?DiscussionOnUsingFinalizers Cheers, Nat.