473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Challenging GotW 66's moral

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
  20. not allocated
  21. }
  22. catch (...)
  23. {
  24. // if we do not delete p, there will be memory leak
  25. // at this point, we are conflicting with Gotw 66's moral 1
  26. if (p) delete[] p;
  27. }
  28. }
  29.  

thanks in advance,
George
Dec 27 '07 #1
4 1231
On Dec 27, 1:54 am, George2 <george4acade...@yahoo.comwrote:
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.

[code]
class A
{
private:

int* p;

public:

A()
try
{
p = new int[10];

// there are some other exceptions here

}
catch (bad_alloc)
{
// do not delete since bad_alloc means memory pointed by p is
not allocated
}
catch (...)
{
// if we do not delete p, there will be memory leak
// at this point, we are conflicting with Gotw 66's moral 1
if (p) delete[] p;
at this point, p would never be 0 (even if the new allocation in ctor
body was not processed yet).
Pointer p, as is, has either a valid address or garbage in it.
As far as that catch block is concerned, it'll always delete [] p.

So how about:

A() : p(0)
try
{
p = new int[10];
}
catch(...)
{
if (p) delete[] p;
}

have you considered shared_ptr instead of doing decadent new
allocations?
Dec 27 '07 #2
On 2007-12-27 07:54, George2 wrote:
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. int* p;
  5. public:
  6.     A()
  7.     try
  8.     {
  9.         p = new int[10];
  10.         // there are some other exceptions here
  11.     }
  12.     catch (bad_alloc)
  13.     {
  14.         // do not delete since bad_alloc means memory pointed by p is
  15. not allocated
  16.     }
  17.     catch (...)
  18.     {
  19.         // if we do not delete p, there will be memory leak
  20.         // at this point, we are conflicting with Gotw 66's moral 1
  21.         if (p) delete[] p;
  22.     }
  23. }
  24.  
The advice assumes that you follow other advices, such as using RAII and
writing exception safe code, if you did you would never end up in a
situation where you need to free any memory in a catch-block. One
example of that would be to replace p with a smart-pointer.

--
Erik Wikström
Dec 27 '07 #3
George2 <ge*************@yahoo.comwrote:
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?
Your idea is covered in the article:

"--But wait!" I hear someone interrupting from the middle of the
room. "I don't agree with Moral #1. I can think of another possible
use for constructor function-try-blocks, namely to free resources
allocated in the initializer list or in the constructor body!"

Sorry, nope. After all, remember that once you get into your
constructor try-block's handler, any local variables in the
constructor body are also already out of scope, and you are
guaranteed that no base subobjects or member objects exist any more,
period. You can't even refer to their names.

Maybe the output to the following will help:

class B {
public:
B() { cout << "B()\n"; }
~B() { cout << "~B()\n"; }
void foo() { cout << "B::foo()\n"; }
};

class A
{
private:
B b;
public:
A() try: b()
{
throw -1;
}
catch (...)
{
b.foo();
}
};

int main() {
try {
A a;
}
catch ( ... ) { }
}

Note that B::foo() is called *after b's destructor has already been
called.* Thus invoking undefined behavior.
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. int* p;
  5. public:
  6.     A()
  7.     try
  8.     {
  9.         p = new int[10];
  10.         // there are some other exceptions here
  11.     }
  12.     catch (bad_alloc)
  13.     {
  14.         // do not delete since bad_alloc means memory pointed by p is
  15. not allocated
  16.     }
  17.     catch (...)
  18.     {
  19.         // if we do not delete p, there will be memory leak
  20.         // at this point, we are conflicting with Gotw 66's moral 1
  21.         if (p) delete[] p;
Expand|Select|Wrap|Line Numbers
  1.  
  2. 'p' doesn't exist once you get in the catch block. Yes, in your example
  3. it happens to still point to the right place, but there is no guarantee
  4. that this is true. Frankly, I'm surprised the code even compiled.
  5.  
  6.         
  7.                     }
  8. }
  9.  
  10.  
>

thanks in advance,
George
Dec 27 '07 #4
On Dec 27, 4:58 am, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 27, 1:54 am, George2 <george4acade...@yahoo.comwrote:
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.
[code]
class A
{
private:
int* p;
public:
A()
try
{
p = new int[10];
// there are some other exceptions here
}
catch (bad_alloc)
{
// do not delete since bad_alloc means memory pointed by p is
not allocated
}
catch (...)
{
// if we do not delete p, there will be memory leak
// at this point, we are conflicting with Gotw 66's moral 1
if (p) delete[] p;

at this point, p would never be 0 (even if the new allocation in ctor
body was not processed yet).
Pointer p, as is, has either a valid address or garbage in it.
As far as that catch block is concerned, it'll always delete [] p.

So how about:

A() : p(0)
try
{
p = new int[10];}

catch(...)
{
if (p) delete[] p;

}

have you considered shared_ptr instead of doing decadent new
allocations?

obviously, clarification is required since you can't allocate an array
using boost::shared_ptr.
replace the array with a std:vector or use boost::scoped_array.
Dec 27 '07 #5

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

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: 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"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.