473,463 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Challenging GotW 66's moral

200 100+
Hello everyone,


In GotW #66, one of the moral is the exception handler of constructor should not do any like resource free task. I do not agree. Here is the quoated moral and my code to prove this moral will have memory leak.

Anything wrong with my analysis?

http://www.gotw.ca/gotw/066.htm

Moral #1: Constructor function-try-block handlers have only one purpose -- to translate an exception. (And maybe to do logging or some other side effects.) They are not useful for any other purpose.


Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. private:
  4.  
  5. int* p;
  6.  
  7. public:
  8.  
  9.     A()
  10.     try
  11.     {
  12.         p = new int[10];
  13.  
  14.         // there are some other exceptions here
  15.  
  16.     }
  17.     catch (bad_alloc)
  18.     {
  19.         // do not delete since bad_alloc means memory pointed by p is not allocated
  20.     }
  21.     catch (...)
  22.     {
  23.         // if we do not delete p, there will be memory leak
  24.         // at this point, we are conflicting with Gotw 66's moral 1
  25.         if (p) delete[] p;
  26.     }
  27. }
  28.  

thanks in advance,
George
Dec 27 '07 #1
4 1058
weaknessforcats
9,208 Expert Mod 8TB
The example is not correct.

The correct code is:
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. private:
  4.  
  5. int* p;
  6.  
  7. public:
  8.  
  9.     A()
  10.     try
  11.     {
  12.         p = new int[10];
  13.  
  14.         // there are some other exceptions here
  15.  
  16.     }
  17.     catch (bad_alloc)
  18.     {
  19.         // do not delete since bad_alloc means memory pointed by p is not allocated
  20.         throw;        //re-throw exception
  21.  
  22.     }
  23.     catch (...)
  24.     {
  25.         // if we do not delete p, there will be memory leak
  26.         // delete p regardless. 
  27.         delete[] p;
  28.         throw;    //re-throw exception
  29.     }
  30. }
  31.  
I have already covered this with you: Heap allocations in the current stack frame are not part of stack unwinding. Therefore, you have to delete the heap allocation. Then you have to either re-throw the original exception or throw a new one based on your exception design in your application.

Catching a bad_alloc might make sense if you are not using Windows - and I have covered that with you.
Dec 27 '07 #2
George2
200 100+
Thanks weaknessforcats,


I agree with all of your points, except the below one. I saw many programs, including MSDN samples, they catch bad_alloc. Why do you think it is bad idea (your previous reason is not strong, I think, even if Windows is using virtual memory, the case when there is bad_alloc still could happen)?


Catching a bad_alloc might make sense if you are not using Windows - and I have covered that with you.

regards,
George
Dec 28 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Why do you think it is bad idea (your previous reason is not strong, I think, even if Windows is using virtual memory, the case when there is bad_alloc still could happen)?
One hour before bad_alloc, you see a message box: "Windows running low on resources". Thirty minutes before bad_alloc, you notice the hourglass appearing briefly here and there, Fifteen nmiutes before bad_alloc the hour glass is on most if the time. You machine response is sluggish. Five minutes before bad_alloc, operations that took the blink of an eys now take ten minutes. The hourslgass is constantly on. You notice the disc activity light is steady on. Your machine starts to vibracte across the floor, etc....

I have never seen a bad_alloc every thown from a VC++ compiled program.

You might try it. Just allocate large amounts in a loop and display the loop counter. See what happens.
Dec 28 '07 #4
George2
200 100+
Thanks for sharing your experience, weaknessforcats!


Maybe some other situation will let us meet with the bad_alloc exception? For example, we allocate a vary large amount of memory than system virtual memory size?

One hour before bad_alloc, you see a message box: "Windows running low on resources". Thirty minutes before bad_alloc, you notice the hourglass appearing briefly here and there, Fifteen nmiutes before bad_alloc the hour glass is on most if the time. You machine response is sluggish. Five minutes before bad_alloc, operations that took the blink of an eys now take ten minutes. The hourslgass is constantly on. You notice the disc activity light is steady on. Your machine starts to vibracte across the floor, etc....

I have never seen a bad_alloc every thown from a VC++ compiled program.

You might try it. Just allocate large amounts in a loop and display the loop counter. See what happens.

regards,
George
Dec 29 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Srini | last post by:
Hello all, I was going thru the GotW archives where I had a doubt in this particular item. http://www.gotw.ca/gotw/027.htm There is a mention about a subtle change to the standard in July...
11
by: George | last post by:
Was my question too challenging for everyone? I thought I would get a much quicker response, complete with witty and (sometimes) condescending remarks Here's my question again, in case you missed...
5
by: Olaf Baeyens | last post by:
I have another problem, maybe it is simple to fix. I have this: byte Test=new byte; But I now want to have a second pointer Test2 to point to a location inside this Test. But with no...
18
by: Frankie | last post by:
I have been hired to go to a former client of mine and train their staff programmers on ASP.NET. These guys have only Mainframe, MS Access, SQL Server, and VB6 desktop application development...
2
by: Wayne | last post by:
This is a copy of a message I previously posted in a Microsoft Access Newsgroup, but it was suggested to me that my problem is ASP related and not Access, and hence I'm posting in this newsgroup now...
5
by: alainpoint | last post by:
Hi, I have what in my eyes seems a challenging problem. Thanks to Peter Otten, i got the following code to work. It is a sort of named tuple. from operator import itemgetter def...
0
by: CoreyWhite | last post by:
Dr. Elsebeth Baumgartner (born May 12, 1955) is a former attorney and current CEO of Cleveland Genomics, Inc. (which provides DNA sequencing services), and has doctorate degrees both in law and in...
4
by: George2 | last post by:
Hello everyone, In GotW #66, one of the moral is the exception handler of constructor should not do any like resource free task. I do not agree. Here is the quoated moral and my code to prove...
0
by: fogleman44523 | last post by:
hh-66 patch kits http://cracks.12w.net F R E E
0
by: puzzlecracker | last post by:
It is per Sutter's GotW #47 article. I don't understand why if a U object is destroyed due to stack unwinding during to exception propagation, T::~T will fail to use the "code that could throw"...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.