I'm reading Scott Meyrses' C++ and come across the following code:
class Lock {
public:
explicit Lock(Mutex *pm) // init shared_ptr with the Mutex
: mutexPtr(pm, unlock) // to point to and the unlock func
{
lock(mutexPtr.get()); // see Item15 for info on “get”
}
private:
std::tr1::shared_ptr<Mutex> mutexPtr; // use shared_ptr
}; // instead of raw pointer
In a footnote, he said that the code is not exception-safe. So in his blog he proposed to modify the constructor of the calss as follows:
explicit Lock(Mutex *pm)
{
lock(pm);
mutexPtr.reset(pm, unlock);
}
That's not clear why this code should work. We call the reset method on the not yet initialized mutextPtr
(we removed the entry from the ctor-initializer). Why won't we get something lie Segmenetation fault
?