From: Chris Thomas Date: 2002-05-28T10:10:14+09:00 Subject: Re: Stymied by Ruby's garbage collector On Sunday, May 26, 2002, at 06:27 AM, Nat Pryce wrote: > From: "Sean O'Dell" >>> [discussion on finalisers snipped] >> Yeah, I know...[a finaliser is] not the same thing [as a destructor]. I > hope we get real destructors >> one day. > Why is it not the same thing? Finaliser closures can do everything that > C++ destructors can do, and avoid the problems that exist with > destructors, such as being able to make an object live again during its > finalisation (or leave dangling pointers in C++). > > What would you want a destructor to do that cannot be performed with > finalisers? C++ destructors are invoked at a known point in time (usually when someone explicitly calls delete on the object). Garbage collection makes object destruction unpredictable -- you do not know when a finalizer will be run. This makes sense as a destructor: void TWindow::~TWindow() { if( IsWindowVisible( fWindowPtr ) && (fWindowPtr != NULL) ) { HideWindow( fWindowPtr ); CloseWindow( fWindowPtr ); } } ... but not as a finalizer, because you want the window to disappear from the desktop when you're done with the object, not at some unknown time in the future when GC decides to clean up. Also, IMHO, since unpredictability leads to difficult-to-find bugs, you should generally avoid using finalizers -- if you have a resource which must be explicitly destroyed, it's best if you can find a way to explicitly destroy it, with timing under your control. Chris