Re: explicit call of constructor and destructor
* Abhishek Padmanabh:
This is crucial, because the
constructor might throw an exception. The placement new expression sets
up an exception handler first of all, which has responsibility for
deallocation, before it calls the constructor for you -- the explicit
syntactical constructor call in the expression is, at the level of
execution, a wrapped call.
Sorry, this is new to me. How could the handler be set up by the
placement new call?
The C++ source code is translated by a compiler to executable code.
The handler has to be in the code wrapping the
placement new call.
I'm sorry, that statement is not meaningful to me.
For the example I have above, that exception
should remain uncaught.
Well, since the example code is not valid C++ code it's pretty much
irrelevant what it "should" do. :-)
In case of an exception from A's constructor
(considering it is not as trivial as I have shown and can throw
something) any deallocation should not happen.
A placement new expression is the only construct in C++ that calls
placement deallocation function.
What's new to you is possibly that placement new is a more general
construct than just "in place construction".
But this is a corner of the language that many are unfamiliar with.
Even professionals. For example, one of the most infamous bugs in
Microsoft's MFC library was a memory leak in debug builds resulting from
failure to define a placement deallocation function: they #define'd
'new' as a placement new expression using a placement allocation
function, with no corresponding placement deallocation function.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?