From: Nat Pryce Date: 2002-05-28T22:34:19+09:00 Subject: Re: Stymied by Ruby's garbage collector I've never missed the lack of destructors in languages other than C++. Perhaps its because in C++, it is idiomatic to use the destructor as part of the object's protocol, while in OO languages with GC, as you say, one implements those parts of the protocol as explicit method calls. I usually only use finalisers to ensure safety in the presence of unexpected exceptions, rather than to clean up objects when no longer needed, as C++ destructors are. In my experience it's much easier to write exception-safe code in Ruby than it is in C++. Swings and roundabouts... Cheers, Nat. ________________________________ Dr. Nathaniel Pryce B13media Ltd. Studio 3a, Aberdeen Business Centre, 22/24 Highbury Grove, London, N5 2EA http://www.b13media.com ----- Original Message ----- From: "Chris Thomas" To: "ruby-talk ML" Sent: Tuesday, May 28, 2002 2:10 AM 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 >