N
Niels Dekker - no reply address
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
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