Comment 13 for bug 1286611

Revision history for this message
Erik Ogenvik (erik-ogenvik) wrote :

I don't understand what you're trying to prove with this snippet. I asked you for an example to validate your assertion that "It is unsafe to assign an pointer that is returned by the new operator directly because the target variable was eventually not initialised with a well-known value like "nullptr" before a C++ exception would occur."
The code you posted on the contrary actually proves that your claim is incorrect.
Since the stack is unwound while creating "x" there will never be a valid "x" instance, and thus there will never be a "bounce" field. Thus there's no risk of ever having to worry about whether "bounce" is valid. It's a binary situation: either no exception is thrown in the constructor, and "bounce" points to a valid instance. Or there is an exception thrown and there's never any valid "x" instance to begin with, and thus never any "bounce" field either.

I agree that unique_ptr is useful for making sure that nested instance memory is correctly released in the event of an exception in a constructor. However, on your assertion that it's unsafe to assign a pointer that is returned by the new operator you must understand that you are incorrect about the memory model and program flow of C++.