3

I have a class inside a class, nested class

class A {
 public:
 int a;
 int b;
  class B {
    int c;
    int d;
  }
}

In the process, I allocate the class B's object pointer in heap.

   B *bobj = new B();
   A *a;
   auto_ptr<A> A1(new A());
   a = A1.release();

Does deleting the a, delete bobj as well??

delete a;

or should we explicitly delete the sub class pointer as well?

This is in a code not written by me, I had put the logs at all alloc and deallocs and I see that B *bobj = new B(), for this there is no delete and for A's object ptr there is a delete. And yet there is no memory leak.That is why I got a doubt what happens in this scenario.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
user862833
  • 289
  • 2
  • 4
  • 16
  • 1
    "In the process.." what process? What does `A* a` point to? – juanchopanza Feb 26 '13 at 12:49
  • @nvoigt it isn't even an access violation, it is a compiler error. – juanchopanza Feb 26 '13 at 12:51
  • I'm afraid just about everything you say is misguided: You're not allocating a pointer on the heap, but rather an *object*. The pointer is an automatic variable. And the word "subclass", terrible as it is, really doesn't mean what you think. If anything, you have a "nested class". – Kerrek SB Feb 26 '13 at 13:20
  • corrected and Added the part of the code. – user862833 Feb 26 '13 at 13:30

4 Answers4

9

I think you're slightly confused here. If you have an object of type A, it doesn't have a B object inside of it. It just has two ints, a and b. Your class B is just declared as a nested class inside A - similar to declaring it in a namespace. It must be referred to as A::B from outside the class (and must be public to do so).

In the sample code you gave, B *bobj = new B(); A *a;, you're not even creating an A object. I presume you meant this (assuming you make B public):

A::B *bobj = new A::B();
A *a = new A();

Both a and bobj are completely separate objects. They have nothing to do with each other. You have to delete both of them.

If instead you had done this:

class B {
  int c;
  int d;
};

class A {
  int a;
  B b;
};

Now an object of class type A has a member called b which is of type B. That b member is part of any object of type A. So if you do A* a = new A();, you get an A object with a B object inside it. You must only do delete a;.

The golden rule: only delete what you have newed.

Joseph Mansfield
  • 100,738
  • 18
  • 225
  • 303
  • 1
    But `B` cannot be instantiated like that, unless that instantiation is inside `A::`. – juanchopanza Feb 26 '13 at 12:50
  • 1
    @juanchopanza Thanks, I missed that. – Joseph Mansfield Feb 26 '13 at 12:52
  • 3
    @juanchopanza Just mentioned that! – Joseph Mansfield Feb 26 '13 at 12:54
  • 1
    @sftrabbit: **The golden rule: do not use `delete`**, and use smart pointers/containers instead. – Matthieu M. Feb 26 '13 at 14:20
  • @sftrabbit: Thanks, That was helpful. – user862833 Feb 27 '13 at 06:59
  • @Matthieu M: Can you please let me know when to use smart pointers and when not to? – user862833 Feb 27 '13 at 07:01
  • 1
    @user862833: the point is not when to use smarter pointers, you always should assuming you need dynamic lifetime; the point is which smart pointer to use. `boost::scoped_ptr`, `std::unique_ptr` and `std::shared_ptr` have different semantics applying to different situations. `scoped_ptr`: for when the resources should not escape the scope, `unique_ptr` when there is a single owner and `shared_ptr` (and its accomplice `weak_ptr`) when there are multiple owners (which is rare). And of course, `std::vector` (or other containers) instead of manual dynamic arrays. – Matthieu M. Feb 27 '13 at 07:45
  • @Matthieu M: I would be more interested in reading it in detail any reference material for the same you can provide, would be of great help.Thankyou again. – user862833 Feb 27 '13 at 08:12
  • 1
    @user862833: see [Is it good to always use smart pointers ?](http://stackoverflow.com/questions/2454214/is-it-a-good-practice-to-always-use-smart-pointers), I believe I did a fairly comprehensive treatment of the issue :) You may also look at [Smart pointers boost explained](http://stackoverflow.com/questions/569775/smart-pointers-boost-explained) for litb's treatment of a similar question. – Matthieu M. Feb 27 '13 at 09:12
3

The way you have written the code, class B is a nested type within class A, but no instance of class B is contained in class A, meaning that the instantiation--and therefore destruction--must be managed separately. So yes, you need to delete both if you new both.

John Zwinck
  • 207,363
  • 31
  • 261
  • 371
3

It depends on what your destructor doing. If A contains pointer of type B and in destructor of A this pointer is released - yes. If A doesn't contains pointer of type B and/or in A's destructor pointer to B is not released - you should release it manually.

ForEveR
  • 52,568
  • 2
  • 101
  • 125
  • Thank you.I wwas trying to delete it in the destructor but the object was declared in the scope of that particular function of class A, so iam not able to delete that object in destructor. – user862833 Feb 27 '13 at 08:14
2

In short, C++ doesn't automatically delete object pointers (without specialize auto-pointers etc.). So, explicitly delete them in your program.

delete a; call destructor of A class. You can write code to delete B's object inside destructor.

Grijesh Chauhan
  • 52,958
  • 19
  • 127
  • 190
mark
  • 5,052
  • 2
  • 18
  • 31
  • "So, explicitly delete them in your program." Yeah, but I'd rather emphasise that it's **better** to use such "auto-pointers etc.", `unique_ptr` being a great choice for many situations. – leftaroundabout Feb 26 '13 at 13:19
  • @mark,@leftaroundabout : can you give me more insight into the smart , auto pointers or reference of any material – user862833 Feb 27 '13 at 08:10
  • 1
    here's one nice description: http://stackoverflow.com/questions/6876751/differences-between-unique-ptr-and-shared-ptr – mark Feb 27 '13 at 13:44