From: MC on
Say I have the following classes

class A{};
class B{};
class C{};

class X{
A* a;
B* b;
C* c;
public:
X() : a(new A), b(new B), c(new C) {}
};

Say now new fails while initializing C. Since X is not constructed
completely it will not call its destructor and we will have a and b
never destructed and hence a memory leak.
What is the best way to avoid this problem.

Should I never use new in the constructor?

Thanks


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
MC wrote:
> Say I have the following classes
>
> class A{};
> class B{};
> class C{};
>
> class X{
> A* a;
> B* b;
> C* c;
> public:
> X() : a(new A), b(new B), c(new C) {}
> };
>
> Say now new fails while initializing C. Since X is not constructed
> completely it will not call its destructor and we will have a and b
> never destructed and hence a memory leak.
> What is the best way to avoid this problem.

By not using raw pointers to hold onto dynamic resources (use a suitable
smart pointer)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: peter koch larsen on
On 3 Mar., 21:40, MC <manan.cho...(a)gmail.com> wrote:
> Say I have the following classes
>
> class A{};
> class B{};
> class C{};
>
> class X{
> A* a;
> B* b;
> C* c;
> public:
> X() : a(new A), b(new B), c(new C) {}
>
> };
>
> Say now new fails while initializing C. Since X is not constructed
> completely it will not call its destructor and we will have a and b
> never destructed and hence a memory leak.
> What is the best way to avoid this problem.
>
> Should I never use new in the constructor?

Not if you have more than one. You get around this problem by using
auto_ptr:
X()
{
auto_ptr<A> aa(new A);
auto_ptr<B> ab(new B);
a = aa.release();
b = ab.release();
}

You will have to expand the example to three parameters. ;-)

/Peter


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Stuart Golodetz on
MC wrote:
> Say I have the following classes
>
> class A{};
> class B{};
> class C{};
>
> class X{
> A* a;
> B* b;
> C* c;
> public:
> X() : a(new A), b(new B), c(new C) {}
> };
>
> Say now new fails while initializing C. Since X is not constructed
> completely it will not call its destructor and we will have a and b
> never destructed and hence a memory leak.
> What is the best way to avoid this problem.
>
> Should I never use new in the constructor?
>
> Thanks

See FAQ 17.4:

http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.4

The executive summary is that you should replace those raw pointers with
smart pointers and your problem will go away.

Regards,
Stu

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
MC wrote:

> Say I have the following classes
>
> class A{};
> class B{};
> class C{};
>
> class X{
> A* a;
> B* b;
> C* c;
> public:
> X() : a(new A), b(new B), c(new C) {}
> };
>
> Say now new fails while initializing C. Since X is not constructed
> completely it will not call its destructor and we will have a and b
> never destructed and hence a memory leak.
> What is the best way to avoid this problem.
>
> Should I never use new in the constructor?
>

Use smart pointers:

class X{
shared_ptr<A> a;
shared_ptr<B> b;
shared_ptr<C> c;
public:
X() : a(new A), b(new B), c(new C) {}
};

The initialization of each member is separately from each other member, so
you are guaranteed that as long as "a" is not yet completely initialized,
the "new B" has not yet been done. On the other side, if something here
throws, any completely constructed member is destructed again. So you no
longer leak memory.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]