
On Dec 8, 2012, at 2:18 PM, Lars Viklund
On Sat, Dec 08, 2012 at 01:34:18PM -0500, Andrew Holden wrote:
That will work unless cptr_open() returns NULL. If I recall, if cptr_open() returns NULL, then the shared_ptr will report it is initialized correctly even though it contains a NULL pointer, and will call cptr_close (NULL) when it goes out of scope.
This is of course not necessarily an error if your deleter is documented to Do The Right Thing on nulls, like 'free', as well as simple wrappers around 'delete' and 'delete[]'.
True, and if cptr_close was written by the O.P., then adding a check for NULL would be trivial and would not have a measurable impact on runtime.
Also note that while it's "initialized correctly", operator bool-ish will still evaluate to false for a held nullptr.
Good point. For whatever reason (I can't remember why) I got into the habit of calling shared_ptr::empty() instead of using the pointer as a bool. You just gave me a reason to revisit that decision.