472,805 Members | 759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

safe for exception code?

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

( pb is a member of class )
WIdget& WIdget::operator=(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?
Dec 30 '07 #1
2 1552
SeniorLee wrote:
in the book EC++ chapter 11, with this code

( pb is a member of class )
WIdget& WIdget::operator=(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
Dec 30 '07 #2
On Dec 30, 12:06 am, SeniorLee <prog3...@gmail.comwrote:
in the book EC++ chapter 11, with this code

( pb is a member of class )

WIdget& WIdget::operator=(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::operator=(const WIdget& rhs)
{
if (&rhs == this)
return *this;
// do assignment stuff
return *this;
}
Dec 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sasha | last post by:
Hi, I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and all... My idea is to have private Guid...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
18
by: digz | last post by:
Hi , I am trying to write a class that encapsulates a Union within it , I intend the code to be exception safe. can someone please review it and tell me if I have done the right thing , are...
4
by: George2 | last post by:
Hello everyone, Here is Bjarne's exception safe sample, http://www.research.att.com/~bs/3rd_safe.pdf template <class Tclass Safe {
4
by: George2 | last post by:
Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant). It used...
18
by: Verde | last post by:
I would appreciate your comments on the following two alternatives of a given method. This isn't a real method, as I'm not concerned about the "real work" it could be doing, but would like to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.