17 May
2011
17 May
'11
1:36 p.m.
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);