0

I've defined three classes, as they can be seen downwards. B is a derived class from A. C is a register class containing A and B objects in an unknown amount. For this reason, I'm using a pointer array dynamically allocated that will store each dynamically allocated object's reference. For example, I created a two-member collection, and printed out the member variables on the output. Despite deleting them (objects first, then the pointer array) afterwards, memory leak is still detected. Can I get some help, cause I really don't see what I'm doing wrong?

#include <iostream>
#include <string>
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

using namespace std;

class A {
protected:
    int a;
public:
    A(int pa) { a = pa; }
    A(const A& theOther) { this->a = theOther.a; }
    virtual void print() const { cout << a << endl; }
};

class B: public A {
protected:
    int b;
public:
    B(int pa, int pb): A(pa), b(pb) {}
    B(const B& theOther): A(theOther.a), b(theOther.b) {}
    void print() const {
        A::print();
        cout << b << endl;
    }
};

class C {
public:
    unsigned elementNum;
    A** pData;
    void createCollection() {
        cin >> elementNum;
        pData = new A * [this->elementNum];
    }
    ~C() {
        for (unsigned i = 0; i < this->elementNum; i++) {
            delete pData[i];
        }
        delete[] pData;
    }
};

int main(void) {
    C REG;
    REG.createCollection();
    REG.pData[0] = new A(2);
    REG.pData[1] = new B(3, 4);
    REG.pData[0]->print();
    REG.pData[1]->print();

    _CrtDumpMemoryLeaks();
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Try with a virtual destructor in A – Artyer May 15 '22 at 13:49
  • 4
    `_CrtDumpMemoryLeaks();` -- Look where this line is located in your program. You placed it before any of your objects had a chance to have their destructors called. And yes, class `A` requires a virtual destructor. – PaulMcKenzie May 15 '22 at 13:53
  • Am I supposed to overwrite A's destructor, or simply creating a virtual default destructor will do the problem? And I realised that it might not be the best idea to put _CrtDumpMemoryLeaks() function before return 0; where the destructor gets called, but if I put it after return 0; it wont get called. How do I overcome this problem? –  May 15 '22 at 13:59
  • If you want to use `_CrtDumpMemoryLeaks()` properly, then enclose the code you have now around `{ }` blocks: `int main() { {C REG; REG.createCollection(); REG.pData[0] = new A(2); } _CrtDumpMemoryLeaks(); }`. This way, the code gets executed locally within the block, and only after the block exits do you call `_CrtDumpMemoryLeaks`. – PaulMcKenzie May 15 '22 at 14:00
  • It's working, thanks for the help. And one more thing, I still don't get why do I need to use virtual destructor in case of class A. –  May 15 '22 at 14:15
  • Because if you have a `B` in the array then `B::~B()` needs to be called. That can only work if `A::~A()` is virtual so it can be overwritten by `B`. More details here: https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – Goswin von Brederlow May 15 '22 at 15:03
  • 1
    @MarcellFehér `REG.pData[1] = new B(3, 4);` -- If you attempt to `delete` the object, the behavior is undefined due to `A` not having a virtual destructor. Deleting a derived object through a base class pointer requires virtual destruction in the base class. – PaulMcKenzie May 15 '22 at 17:07
  • `A** pData` is an insta-fail. We don't do such things in this current millennium. Use `std::vector>` (or `std::vector>` if sharing is needed). – n. m. could be an AI May 15 '22 at 18:57

0 Answers0