From: Mark Hahn Date: 2001-11-25T07:38:25+09:00 Subject: [ruby-talk:26374] Re: [OT] Re: KDE or GNOME curiosity question... How hard would it be to have an option to use reference counting in a particular class? This class would be handled normally by the garbage collector, but instances of this class would have a method called immediately when the last reference is gone (before the finalizer). Something like: class File2 < File def you_are_now_an_orphan # do destructiion duties end end you_are_now_an_orphan (or a better name) would be a special method whose presence would indicate reference counting should be used. -----Original Message----- From: Bill Kelly [mailto:billk@cts.com] Sent: Saturday, November 24, 2001 11:21 AM To: ruby-talk ML Subject: [ruby-talk:26360] Re: [OT] Re: KDE or GNOME curiosity question... From: "Yukihiro Matsumoto" > [...] > First of all, what does RAAI stand for? I've been interpreting it as (a typo for) RAII, the idiom of "Resource Acquisition is Initialization" used in C++. [[For the really curious, here's a thread on Usenet where people discuss the finer points of what RAII "really means" in C++ in exquisite, laborious, gory detail :-) http://groups.google.com/groups?hl=en&threadm=B5291D12.4F4C%25abrahams%40med iaone.net&rnum=1 ]] My take on it is it's applicability basically stems from C++'s distinction between objects created "locally" on the stack frame, which have a definite "out of scope condition" at the point their semantic 'block' is exited in the code, and objects which are allocated on the heap, which are only contained in pointers (be they smart or otherwise.) That is, it's an extra distinction about objects, or object allocation, that languages like Ruby or Java don't have. . . So C++'ers utilize this guaranteed "out of scope destruction" of locally allocated objects to implement things like smart pointers (such as std::auto_ptr<>) which can perform an automatic delete on the type of (heap allocated) object to which they point, as the smart pointer itself (a locally allocated stack frame object) goes out of scope. So the meaning of RAII to me is essentially "acquire resources during initialization in such a way that cleanup occurs automatically" as we leave a particular scope block or an exception is thrown, etc...... One other point comparing C++ to a GC'd language is that I personally find only a fraction of the objects I create in a GC'd language require special treatment in their cleanup. That is in C++ you have to ensure the cleanup of _everything_, where is in GC'd languages one need only ensure deterministic cleanup of objects holding non-memory resources. Which in my experience has only been some fraction of the objects I create. So the whole thing for me is a kind of trade-off: meticulous adherance to RAII in C++ _all the time_, vs. attending to cleanup of only certain resource-containing objects in a GC'd language. And as Rubiers have mentioned, both blocks and begin/ensure provide a way of handling such cleanups . . . Regards, Bill