473,503 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple calls to new in member initializer list okay?

The book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu
warns against potential memory leaks when having multiple calls to
operator new within a single statement. (Item 13, page 25.) These
leaks would still be there when wrapping the result of each "new" in a
temporary smart pointer object.

Is there a simular risk when having multiple calls to new in member
initializer list of a constructor? For example:

class Foo
{
// I might use either std::auto_ptr or boost::scoped_ptr.
const std::auto_ptr<A> m_a;
const std::auto_ptr<B> m_b;
public:
Foo() : m_a(new A), m_b(new B) {}
};

Is the compiler allowed to do:
1. allocate memory for m_a
2. allocate memory for m_b
3. call the constructor of A
4. call the constructor of B

And then, is it allowed to leave a memory leak when the constructor of A
throws an exception?

Kind regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 23 '05 #1
1 2929
Niels Dekker - no reply address wrote:
The book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu
warns against potential memory leaks when having multiple calls to
operator new within a single statement. (Item 13, page 25.) These
leaks would still be there when wrapping the result of each "new" in a
temporary smart pointer object.

Is there a simular risk when having multiple calls to new in member
initializer list of a constructor? For example:

class Foo
{
// I might use either std::auto_ptr or boost::scoped_ptr.
const std::auto_ptr<A> m_a;
const std::auto_ptr<B> m_b;
public:
Foo() : m_a(new A), m_b(new B) {}
};

Is the compiler allowed to do:
1. allocate memory for m_a
2. allocate memory for m_b
3. call the constructor of A
4. call the constructor of B

And then, is it allowed to leave a memory leak when the constructor of A
throws an exception?

Kind regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware


This looks okay to me, the reason being that there is a sequence point
between "m_a(new A)" and "m_b(new B)" (that is, the first expression is
fully evaluated before the second is considered).

If the constructor of A throws an exception, "new A" will never
complete, so no memory will be leaked. If an exception is thrown after
m_a is fully constructed then the compiler will ensure that m_a is
properly destructed, so no leaks there either. The same argument
applies to B and m_b, so I would conclude that this usage is fine.

However, there is a similar usage that is not exception safe (just in
case you haven't encountered it):

Foo(std::auto_ptr a, std::auto_ptr b)
: m_a(a), m_b(b)
{}

// ...

// NOT EXCEPTION SAFE!!
Foo f(std::auto_ptr<A>(new A), std::auto_ptr<B>(new B)) ;

Here, there is NOT a sequence point between the two calls to new, so the
compiler may choose to execute "new A", then "new B", and then construct
the auto_ptr objects, but an exception thrown during "new B" would cause
the memory from "new A" to be leaked.

-Alan
Jul 23 '05 #2

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

Similar topics

2
2501
by: Jeff Massung | last post by:
I am having a syntax issue that I hope someone else here knows how to rectify... I am loading an INI file and have a simple function to load values from it. The function is overloaded with the...
2
3076
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
3
1591
by: santosh | last post by:
Hello, I have const member in the class. How can I initialise these. I can not initialise in constructor ,(it is giving compilation error) What is the proper way to initialise. The code is given...
2
1420
by: William Ahern | last post by:
Is the following legal, and if so is the behavior specified? struct s { int a; const char *b; }; int main(void) { struct s test = { .a = 12,
6
1511
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
4
1454
by: Ham Pastrami | last post by:
How do you initialize objects in the initializer list? Also, while this code is probably incorrect, it does compile (and execute) and I wonder what the actual result is. class Control { Point...
7
3713
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
3
2136
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
15
7838
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
7193
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
7264
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
7316
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
6975
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
7449
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...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.