safe for exception code?

S

SeniorLee

in the book EC++ chapter 11, with this code

( pb is a member of class )


WIdget& WIdget::eek:perator=(const WIdget& rhs)
{
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;

return *this;
}



book says this code is safe for exception, but what if exception
occurs in " pb = new Bitmap(*rhs.pb);" this line?

and it's going to run the next line(delete pOrig;). this will result
this object lose its bitmap pointer.

this is right? or wrong?
 
J

jkherciueh

SeniorLee said:
in the book EC++ chapter 11, with this code

( pb is a member of class )


WIdget& WIdget::eek:perator=(const WIdget& rhs)
{
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;

return *this;
}



book says this code is safe for exception, but what if exception
occurs in " pb = new Bitmap(*rhs.pb);" this line?

and it's going to run the next line(delete pOrig;). this will result
this object lose its bitmap pointer.

this is right? or wrong?

Wrong: if

pb = new Bitmap(*rhs.pb);

throws, flow control directly reverts to the corresponding catch-handler.
Consequently, the next line

delete pOrig;

will not be executed in the case of a throw.

A more tricky question is what happens to the variable pb if new succeeds to
allocate memory but the constructor of Bitmap throws. I have a vague
recollection of a discussion on comp.std.c++ regarding this. The standard
was not all that clear to me, but it appears that the intend (if not the
wording) of the standard is that the value of pb remains unchanged if the
rhs throws during evaluation. Thus, the code above should be
exception-safe.


Best

Kai-Uwe Bux
 
S

Salt_Peter

in the book EC++ chapter 11, with this code

( pb is a member of class )

WIdget& WIdget::eek:perator=(const WIdget& rhs)
{
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;

return *this;

}

book says this code is safe for exception, but what if exception
occurs in " pb = new Bitmap(*rhs.pb);" this line?

and it's going to run the next line(delete pOrig;). this will result
this object lose its bitmap pointer.

Your arguement is mute, if that new allocation throws, everything
after new is skipped, including the return.
Furthermore, the program will unwind each calling scope(s)'s stacks
progressively until the exception is caught ( or in the event thrown
exceptions are left uncaught: terminate() gets called ).
this is right? or wrong?

Its right, assuming that member pb was initialized appropriately
beforehand.
Also, that assignment operator should be checking for self-assignment.

WIdget& WIdget::eek:perator=(const WIdget& rhs)
{
if (&rhs == this)
return *this;
// do assignment stuff
return *this;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top