[Correction:] This is not so. An exception in your constructor will not be a resource leak, because the only place where an exception can occur is inside the new expression, and if a new expression is not executed, the resources that were allocated to it are freed. Your situation is special, because you only make one selection in the constructor - in general this is unsafe!
Your quoted phrase refers to the delete operator for a failed object whose constructor threw:
struct T { T() { throw 1; } char data[200]; };
On the last line, memory is allocated before the constructor is called. This memory is freed in the event of an exception by automatically calling ::operator delete(pt) . (In general, the corresponding delete operator is called (not the "expression"!), Which corresponds to the new expression.)
This happens as follows:
Please note: we only have an object after the constructor completes, therefore, in case of an exception, we do not even have an object in the constructor. This is why I said “unsuccessful object” above with a hyphen because it is not an object at all (for example, Douglas Fir is not fir at all).
Your code potentially runs completely unsafe if you make more than one distribution that can be thrown, that is, a leak occurs whenever one object was successfully constructed and another, the next one is not executed. You probably just can't call new in the initializer list and instead put it in the body:
class Danger { T * pt1, * pt2; public: Danger() { try { pt1 = new T; } catch(...) { throw(); } try { pt2 = new T; } catch(...) { delete pt1; throw(); } } };
Or, on a sole responsibility basis, do not use raw pointers, but use resource management containers that are cleared after themselves !!