I have an abstract class. I'm creating a large array in the base class, which appears to never be deleted because no destructor is called. My questions (obviously) are...
- Why isn't a destructor called?
How do I fix it?
#include <iostream> using namespace std; class A { protected: int* b; public: A() { cout << "ANull\n"; b = new int[1000000]; } ~A() { cout << "ADest\n"; delete b; } virtual void foo() = 0; }; class B: public A { private: static int c; public: B() { c++; b[5] = c * 2; cout << "BNull\n"; } ~B() { cout << "BDest\n"; } void foo() { cout << "B(b[5],c) = (" << b[5] << "," << c << ")\n"; } }; int B::c = 0; int main() { for(int i = 0; i < 1000000; i++) { A* obj = new B(); obj->foo(); } cin.get(); return 0; }
Output
ANull
BNull
B(b[5],c) = (2,1)
ANull
BNull
B(b[5],c) = (4,2)
ANull
BNull
B(b[5],c) = (6,3)
ANull
BNull
B(b[5],c) = (8,4)
ANull
BNull
Etcetera. Neither "ADest" nor "BDest" is ever displayed. The behavior is that the loop whizzes by till c gets to be about 300, then things slow down, which seems like what you'd expect for a big memory leak, which appears to be what's happening since the destructor is not being called.