Re: throwing dtors...
In article <X5_Ek.12946$ex3.3200@newsfe02.iad>,
Chris M. Thomasson <no@spam.invalid> wrote:
"anon" <anon@no.invalid> wrote in message
news:gc1n78$kks$1@news01.versatel.de...
Chris M. Thomasson wrote:
Is it every appropriate to throw in a dtor? I am thinking about a simple
example of a wrapper around a POSIX file...
Take a look here:
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.13
Okay; I will give it a look.
[...]
How would you handle it in C?
[...]
What can you do when fclose fails?
Well, it depends on the error:
http://www.opengroup.org/onlinepubs/007908775/xsh/fclose.html
For instance, fclose can get interrupted by a signal. In this case, you need
to reissue the operation; e.g:
______________________________________________________________
struct file {
FILE* handle;
};
int file_close(
struct file* const this
) {
int status;
do {
status = fclose(this->handle);
} while (status == EOF && errno == EINTR);
return status;
}
That's precisely your answer:
Do exactly what you would do in C in your file_close method
and call it from your destructor:
File::~File()
{
if(0 != file_close()) {
// log the error
// or std::terminate?
}
}
int File::file_close()
{
int status = 0;
do {
status = fclose(handle);
} while (status == EOF && errno == EINTR);
return status;
}
Or, what if it returns `EAGAIN', well, this certainly needs to be handled.
So handle EAGAIN. 2 ways:
1- in file_close()
2- if impossible in file_close() then you must require that the client do
it by explicitely calling file_close() and dealing with errors.
Fine... Now, a user needs to copy a file to a disk, and destroy the
original. Okay. It creates two file objects (e.g., src and dest:)
{
file src(...);
file dest(...);
// Then it performs the copy operation:
[copy src to dest]
}
Now the code-block goes out of scope, and no exceptions were thrown during
the copy process, HOWEVER, the call to `fclose()' in the dest object
failed!!!! Well, the user thinks everything is fine because the completely
retarded ignorant moron file object did not report the fuc%ing error! So the
user operates in ignorance and happily destroys the original file thinking
that the file was COMPLETELY copied onto the disk! WRONG! The file was
partially copied because `fclose()' failed to do its thing and properly
flush the buffers, or whatever... Now, the missing file data is LOST
__forever__! OUCH!!!
That's a failure of the application
foo()
{
file src(...)
file dest(...)
dest.copy(src);
// Must ensure that dest was correctly flushed/sync/close *before*
// deleting src
if(0 == dest.file_close())
{
src.delete();
} else {
// whatever is needed to recover
}
}