Boost logo

Boost :

From: Gregory Dai (gregory.dai_at_[hidden])
Date: 2007-11-03 02:35:55


On 11/2/07, Peter Dimov <pdimov_at_[hidden]> wrote:
>
> Mike Tegtmeyer:
>
> > The counter argument that I hear from not-new folks however is that it
> > takes some work to not leak the resource during an exception in the
> > shared_ptr construction. Therefore, the ugliness to maintain integrity
> > diminishes it's usefulness. ie
> >
> > FILE *in = fopen(...);
> > if(!in)
> > return/throw badness;
> >
> > //may throw here, 'in' is lost
> > shared_ptr<FILE> fin(in,fclose);
>
> The answer here is much, much easier. 'in' will not be lost. shared_ptr
> does
> invoke the deleter when the constructor throws.

I don't think making file_entry is a lot of work. It's pretty simple and
straightforward. And once it's made, you keep it around in your arsenal.

I still prefer

shared_ptr<file_sentry> file(new file_sentry(fopen(...));

to

FILE *in = fopen(...);
if (!in)
    return/throw badness;

shared_ptr<FILE> fin(in,fclose);


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk