Paul Vixie <> writes:
> bert hubert wrote:
>> The RAII technique goes like this:
>> struct ReadFP : boost::non_copyable
>> {
>>      ReadFP(std::string&  fname)
>>          {
>>             d_fp = fopen(fname.c_str(), "r")
>>             if(!d_fd)
>>              throw runtime_error("Opening file "+fname+": "+strerror(errno);
>>      }
>>      ~ReadFD()
>>      {
>>              fclose(d_fp);
>>      }
>>      FILE* d_fp;
>> };
>> Written like this, you'll never leak an fd 'ever'.

Not to pick on Bert, but "not leaking" isn't the only figure of merit.
This programming technique ignores the possibility that fclose might
fail ... or perhaps more usefully, that you might want to be sure that
you fsync output files before closing them, and that you definitely
ought to notice errors from that fsync.  What do you do about errors
during resource cleanup?

This is a hard problem, and I don't mean to imply that there's an
obvious way to do better.  But sweeping such issues under the rug
doesn't serve anyone.

                        regards, tom lane


Reply via email to