0

I have a class:

class A {
    A();
    ////////something about the class A
};

then I have another class:

class B {
public:
    B();
    A* member_a;
};
B::B()
{
    this->member_a = new A();
}

what I want to do is like this:

main()
{
    vector<B> vec_b;
    int num=1;
    while(some_condition)
    {
        for (int i=0; i<num; i++)
        {
            vec_b.pushback(B());
        }
        ////////do something about vec_b;
        num++;
    }
}

I know I am facing a memory leak issue because of the new A() in the constructor of class B. So I am trying to get help to release the memory after each while loop, which means to recycle all the memory that has been taken by the vec_b and all the objects of class B in the vector, the most important is to release the memory taken by class-A-objects. Thank you very much!

himmel
  • 19
  • 2
  • 3
    You *do* know about destructors, right? And how to use the `delete` keyword? And if you always allocate in the constructor, why allocate dynamically at all? Don't use pointers if you don't actually have to. – Some programmer dude Feb 08 '16 at 12:07
  • "You wrote "I am trying to get help". This is not a question. Good luck in your quest to get some help. stackoverflow.com is where you get to ask specific questions on various topics. It is not where you go to get help with your code. If you do indeed figure out how to phrase a specific question, be sure to edit your post accordingly. – Sam Varshavchik Feb 08 '16 at 12:08
  • 1
    And if you don't know about destructors or the `delete` keyword, then I suggest you [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over. – Some programmer dude Feb 08 '16 at 12:09

3 Answers3

0

That seems like it should be the job of B's destructor

B::~B()
{
    delete member_a;
}

If you have access to C++11 or later, I would prefer std::unique_ptr so you get this deallocation for free

class B {
public:
    B();
    std::unique_ptr<A> member_a;
};

B::B()
{
    member_a = std::make_unique<A>();
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 2
    `std::make_unique` is not available in `C++11`. It is C++14 feature – dlmeetei Feb 08 '16 at 12:12
  • oh I didn't see your updated answer ,ya the same I thought – evk1206 Feb 08 '16 at 12:35
  • If no C++11 available, `std::auto_ptr` (possibly `const`) or `boost::scoped_ptr` can be used. Raw owning pointers should be very strongly discouraged. – Tadeusz Kopec for Ukraine Feb 08 '16 at 12:39
  • Thanks, `make_unique` is not available for me. I tried to add `delete member_a` at the destructor, but the destructor is called when every 'for' loop is finished. at the second time of calling destructor, there will be a 'debug assertion failed' error. – himmel Feb 08 '16 at 13:48
  • Using unique_ptr is pointless in containers since memory is handled automatically therein. – Poriferous May 17 '16 at 09:59
  • @Poriferous In this particular case, you could store the object by value. But if you want the container to store a polymorphic object, then you must store a pointer, in which case a unique_ptr is certainly the way to go. – Cory Kramer May 17 '16 at 11:23
  • @CoryKramer But you don't have to store it in a `unique_ptr`! They are useless in containers and add unnecessary overhead to the CPU. You can just store the items with raw pointers like so: `vector`. The compiler couldn't care less if B were raw or had lube, it only cares if it can do its job as the programmer wanted. – Poriferous May 17 '16 at 14:04
  • @Poriferous Except when the container falls out of scope it won't delete any of those raw pointers, so you're back to square one and didn't answer the OPs question.... – Cory Kramer May 17 '16 at 14:15
  • @CoryKramer They it shouldn't have been declared in scope to begin with. Besides, the container is declared within the main function so when the program is destroyed so is all the memory. :-)) – Poriferous May 17 '16 at 20:29
0

Write the delete in the destructor of class b

B::~B
{
delete member_a;
}
evk1206
  • 433
  • 7
  • 18
0

From the way you use the instance of the class A in your example, it seems to me that you don't actually need a pointer to A at all.

  • If (and only if) you intend to create sub classes of the class A, then you should use a std::unique_ptr for the member instead of a raw pointer.
  • If you don't intend to sub class the class A, then the best option for you is to simply create a solid instance of A inside the class B.

like this:

class B {
public:
    B();
    A member_a;
};

Having a solid instance of A inside the B class saves you from a lot of trouble with memory management.