Boost logo

Boost Users :

Subject: Re: [Boost-users] Segmentation fault when throwing exception?
From: Igor R (boost.lists_at_[hidden])
Date: 2011-05-17 09:36:07


> I have a shared_ptr<FILE> f(fopen(file_that_doesnt_exists, "rb"),
> &fclose); call.
>
> If fopen returns null I think that the custom deleter is still called
> even if the class was constructed with a null pointer.
> Maybe I should make sure myself that fopen doesn't return NULL,
> because, anyway when you construct a shared_ptr
> you are passing a valid resource and it is not checked.

fclose is just a bad deleter that doesn't work correctly with NULL,
but you can wrap it with your own deleter:
void checked_fclose(FILE *f)
{
  if (f)
    fclose(f);
}
shared_ptr<FILE> f(fopen(file_that_doesnt_exists, "rb"), &checked_fclose);


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