From: Jim Weirich Date: 2002-02-24T21:53:24+09:00 Subject: Re: Object/Memory Management >>>>> "Sean" == Sean O'Dell writes: Sean> Actually, wouldn't your example cause the object to never Sean> get cleaned up because of the reference to it, and wouldn't Sean> the object be dead by then anyway? There is no reference to the FinalizingObject. It is created in the scope of a function and immediately forgotten, therefore it should be dead when the GC checks it. There is a referece to the SillyResource in the FinalizingObject and in the closure registered with ObjectSpace. When the FinalizingObject is reaped by the GC, the closure is used to close the SillyResource. Sean> What I thought would work better would be to just store the Sean> information that SillyResource encapsulates and then use the Sean> finalize enclosure to take care of the resources. That's basically the intent of the demo, except the focus is on the FinalizingObject, rather than SillyResource. We do store the information that FinalizingObject encapsulates and use the closure to take care of the resource. Sean> FinalizingObject does a good job of cleaning up the resource Sean> and it doesn't really matter if it ends up hanging around Sean> until garbage collection because we're already used to Sean> letting objects float around in memory unused for awhile. Agreed. The demo's intent was to demonstrate how to use the finalizer. Because of the non-determinism in finalization, I almost never use it as a front line approach to resource management. -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)