-
July 31st, 2007, 03:41 AM
#1
'new' and a ctor that throws.
Is it true, that when I create an instance of an object with 'new' and the ctor throws, the default operator new will deallocate the memory? And if yes, does it call my dtor?
I am just curious because I have a class which does a lot of new'ing in the ctor, and I am aware of the fact that new my throw an exception I have to catch.
Best,
Florian
-
July 31st, 2007, 03:46 AM
#2
Re: 'new' and a ctor that throws.
Yes, the memory will be released; no the destructor will not be called.
An object's lifetime starts on successful exit of a constructor and ends at entry to the destructor. If a ctor throws, the object has never existed, so its dtor will not be called. (Note that the dtors of any member variables that have been fully constructed will be called.)
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 31st, 2007, 03:52 AM
#3
Re: 'new' and a ctor that throws.
Thank you Graham. Indeed my nightmare came true.
I have a container class storing an huge amount of related data. To avoid a clumsy program I use 'new' to allocate the data when only needed. But lets say:
Code:
class Foo
{
ISomething *m, *n, *o, *p, *q;
public:
Foo ( void )
{
m = new Something;
n = new Something; // [1]
o = new Something;
// p and q will be allocated later
}
};
So if the new at [1] throws the space allocated by 'm' will not be released, thus leading to a memory leak?
But by your definition:
Note that the dtors of any member variables that have been fully constructed will be called.
If I use std::auto_ptr around m I can avoid this, right? Since the auto_ptr is fully constructed at the point where n fails to allocate.
-
July 31st, 2007, 04:20 AM
#4
Re: 'new' and a ctor that throws.
auto_ptr or shared_ptr will help with your problem. The reason of all your troubles is that you missed following guideline: one class should be responsible for managing at most one resource. This topic is covered at one of GotW chapters, but I cannot remember which one
Cheers
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
July 31st, 2007, 04:37 AM
#5
Re: 'new' and a ctor that throws.
-
July 31st, 2007, 04:44 AM
#6
Re: 'new' and a ctor that throws.
Originally Posted by NoHero
/btw: Nice to read from you Hobson
Same here, glad to see you back
Cheers
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
July 31st, 2007, 04:50 AM
#7
Re: 'new' and a ctor that throws.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
July 31st, 2007, 05:13 AM
#8
Re: 'new' and a ctor that throws.
Originally Posted by Hobson
Thank you very much, very helpful
And thank you Graham for clearing this up for me
-
July 31st, 2007, 08:01 AM
#9
Re: 'new' and a ctor that throws.
Originally Posted by NoHero
If I use std::auto_ptr around m I can avoid this, right? Since the auto_ptr is fully constructed at the point where n fails to allocate.
Yes, but... I wouldn't use auto_ptr to manage resources for a class, unless you explictly disable copying. If you use auto_ptr, you'll just transfer all the caveats of using auto_ptr itself to your class - can't use it in a standard container, etc...
As Hobson says, shared_ptr is a better bet because it doesn't tie you down with potentially infuriating restrictions.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|