<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
Chandrasekhar Ramakrishnan wrote:
<blockquote cite="mid200406271902.i5RJ20f1052138@soda.csua.berkeley.edu"
 type="cite">
  <pre wrap="">Yes, but you could also do this:

class ZooWriter {
        FileHandle mFH;
public:
        void write_zoo() {
                mFH = open("/adir/afile", open);
                mFH << "I went to the zoo";
        }

        void after_writing_zoo_write_home() {
                mFH << ". Then I went home";
        }

        void essay() {
                write_zoo();
                after_writing_zoo_write_home();
        }
}

And then you'll have a problem.  
  </pre>
</blockquote>
Well, because you've chosen to declare mFH in the object, it doesn't go
out of scope in the above. However, if ZooWriter goes out of scope then
it could (if you make appropriate changes to ZooWriter).<br>
<blockquote cite="mid200406271902.i5RJ20f1052138@soda.csua.berkeley.edu"
 type="cite">
  <pre wrap="">(Aside -- how would you implement the open function described in your
example?</pre>
</blockquote>
It's essentially optional reference counting; so
returning it would increment it an extra time before it goes out of
scope since that is a reference too.<br>
<blockquote cite="mid200406271902.i5RJ20f1052138@soda.csua.berkeley.edu"
 type="cite">
  <pre wrap=""> Instead of creating an open function, I would have given
FileHandle a two argument constructor and written FileHandle f("path",
open), but there's always more I can learn about C++.)
  </pre>
</blockquote>
It was dog C++ code; I was just trying to express the idea of closing a
resource automagically; I think you got the idea.<br>
<br>
My C++ is rusty, but IRC you can do it either way, but your way is
cleaner.<br>
<blockquote cite="mid200406271902.i5RJ20f1052138@soda.csua.berkeley.edu"
 type="cite">
  <blockquote type="cite">
    <pre wrap="">But this is unreliable in Java/Self/Smalltalk- you don't know when the 
GC will get around to release it. I consider this to be a bug in these 
languages.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
Yes, but if your runtime uses a two-space GC implementation and
collects recently allocated objects more frequently, the file handle
will be closed reasonably soon,</pre>
</blockquote>
That's a big if; and GC rate depends on the rate of object creation in
many cases. A generational technique is more popular these days I
think, and can result in quite a long delay in the worst case.<br>
<blockquote cite="mid200406271902.i5RJ20f1052138@soda.csua.berkeley.edu"
 type="cite">
  <pre wrap=""> and you get the added bonus that the
example I provided won't cause any problems.</pre>
</blockquote>
I'm not saying that finalisation functions are forbidden! I'm just
saying that in quite a lot of cases this is a very useful pattern; and
I've used it extensively in the past. No design patterns are
universally applicable.<br>
<br>
And although it is essentially the same technique as reference counting
pointers, I don't see this as being a bottleneck on efficiency- it's
unlikely that the reference/dereference calls would be as frequent.<br>
<blockquote cite="mid200406271902.i5RJ20f1052138@soda.csua.berkeley.edu"
 type="cite">
  <pre wrap="">- sekhar

--
C. Ramakrishnan        <a
 class="moz-txt-link-abbreviated" href="mailto:cramakrishnan@acm.org">cramakrishnan@acm.org</a>
  </pre>
</blockquote>
<br>
</body>
</html>