From: Pete Becker on
jaybus56 wrote:
>
> I would do it this way because I prefer initializer lists (RAII):
>
> class A{};
> class B{};
> class C{};
>
> class X
> {
> auto_ptr<A> a;
> auto_ptr<B> b;
> auto_ptr<C> c;
>

But there are very few situations in which the wierd copy and assignment
semantics of a class like this are useful. It certainly isn't a general
replacement for a class holding raw pointers.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

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

From: Kirill_Lykov on
On Mar 4, 2:40 am, 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?

{ edits: top-posting rearranged, quoted banner removed. please keep readers in
mind. -mod }

The best way to sort out with this problem is to use smart pointers.
However, you shouldn't forget about try/catch block in the mem-
initializer-list.
For instance,
X()
try : a(new A), b(new B), c(new C) {}
catch(const A_exception&) { /*do smth usefull*/ }
catch(const B_exception&) {delete a;}
catch(const C_exception&) {delete a; delete b;}

Yet in general situation this approach doesn't work due to you don't
know
which pointers have been initialized. It means that you can't just
call delete a/b/c instead of /*do smth usefull*/
because they may contain dump.


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

From: Mathias Gaunard on
On Mar 3, 8:40 pm, 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?

Rather, you should avoid using naked pointers.
Why are you even using pointers as members here? It means you will
have to rewrite the assignment operator, the copy constructor and the
destructor.

If you used a ready-made smart pointer or a container, you wouldn't
have to do any of those things and everything would be safe
automatically.


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

From: Nevin :-] Liber on
In article
<94a8aa4d-a4e5-4ede-9794-b7d0f28d6c03(a)t20g2000yqe.googlegroups.com>,
jaybus56 <busch.juergen(a)gmx.net> wrote:

> class X
> {
> auto_ptr<A> a;
> auto_ptr<B> b;
> auto_ptr<C> c;
>
> public:
> X() : a(new A), b(new B), c(new C) {}
>
> // if it is necessary to keep the memory allocation:
> ~X()
> {
> a.release();
> b.release();
> c.release();
> }
> };

No. The destructor should not be releasing the objects, as that will
result in a memory leak. The only time release() should be called is
when ownership is being transferred.

--
Nevin ":-)" Liber <mailto:nevin(a)eviloverlord.com> 773 961-1620

[ 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 4 Mar., 11:24, jaybus56 <busch.juer...(a)gmx.net> wrote:
> On Mar 4, 3:07 am, peter koch larsen <peter.koch.lar...(a)gmail.com>
> wrote:
>
> > Not if you have more than one. You get around this problem by using
> > auto_ptr:
>
> agreed, but
>
> > X()
> > {
> > auto_ptr<A> aa(new A);
> > auto_ptr<B> ab(new B);
> > a = aa.release();
> > b = ab.release();
>
> > }
>
> I would do it this way because I prefer initializer lists (RAII):
>
> class A{};
> class B{};
> class C{};
>
> class X
> {
> auto_ptr<A> a;
> auto_ptr<B> b;
> auto_ptr<C> c;
>
> public:
> X() : a(new A), b(new B), c(new C) {}
>
> // if it is necessary to keep the memory allocation:
> ~X()
> {
> a.release();
> b.release();
> c.release();
> }
>
> };

You could do it that way, but it changes the behaviour of the class.

/Peter


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