On Jun 21, 12:12 am, indrawati.ya...@gmail.com wrote:
According to the FAQ, the best way to inform a class user of an error
that occurs inside a constructor is to throw an exception. My question
is, what happens when an object is instantiated using 'new', and the
constructor throws after 'new' allocates memory for that object? Will
the memory leak?
If it is a non-placement new, the global operator delete
function will be called for the memory. (Watch out for memory
that was allocated within the constructor before throwing,
however.) If it is a placement new, and a corresponding
placement operator delete function exists, it will be called.
If no corresponding placement operator delete function has been
declared, the compiler assumes that it isn't necessary. (For
some reason, the standard defines a no-op placement operator
delete function for the standard placement new, rather than
count on the fact that if there is no corresponding placement
operator delete function, the compiler will do nothing. I
suppose it does prevent the user from accidentally defining one
that does something, but I'll admit that users accidentally
defining a placement delete that shouldn't have been defined has
not been a major cause of error in the code that I've seen.)
--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34