Boost logo

Boost Users :

From: Mauricio Gomes (mg_at_[hidden])
Date: 2005-01-21 16:57:39


> What is the best way to ensure that the file descriptor is
> automatically
> close()ed when the function is exited?

Hi Aaron,

I would create a class (File or other name) that close() on its
destructor.
The constructor can take an additional boolean parameter to configure
the error handling with default value to throw exception (personally I
would not add that, I would always throw the exception and leave to the
user to catch it if he/she wants to do so. I just mentioned it because
it seems you would like the solution to handle that).

This way the file would be closed upon leaving the scope.
Stroustrup talks about that on his "The C++ Programming Language" 3rd
ed. in 14.4.1 (page 366 on my edition) and calls it "resource
acquisition is initialization".

I think this can be useful and reusable.
I am not familiar with boost file system library and I don't know if it
has already something like this that would solve your problem.

You might have thought of that already.
If you did, please share your comments on why this would not be enough.

Best regards,
Mauricio Gomes
Pensar Digital
phone: 55-11-4121-6287
mobile: 55-11-8319-9610
http://pensardigital.com

On Jan 21, 2005, at 6:39 PM, Aaron W. LaFramboise wrote:

> I was writing some simple code today, and rediscovered this general
> sort
> of problem, and realized I have not yet decided on an optimal solution.
>
>
> Given:
> 1) During a particular function, a type-int variable is initialized
> with
> a value corresponding to a POSIX file descriptor.
> 2) There are multiple paths of exit from the function, including both
> returns and exceptions.
>
> Problem:
> What is the best way to ensure that the file descriptor is
> automatically
> close()ed when the function is exited?
>
> An additional thorn is that there must be code for handling that case
> where close() fails. The solution must handle both cases where I'd
> like
> to throw an exception, or cases where I'd just like to print an error
> message and continue.
>
>
> I have considered various solutions, some involving throw away classes,
> and none of them appear to be satisfactory.
>
>
>
> So far, the best solution I have is to create a reusable helper (based
> on templates and boost::function) that is created as an automatic
> variable and is passed the file descriptor, and a functor (which may be
> a boost.lambda expression) that calls close(), and contains any other
> necessary error-processing code. But seriously, for such a simple
> problem, this is insane!
>
> Solutions I don't particularly like involve creating a non-reusable
> throw-away class that handles just this one particular case, or lack
> sufficient mechanisms for handling errors.
>
>
> Any ideas?
>
>
> Aaron W. LaFramboise
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net