I am doing a win32 application in that i am using 2class.From one class constructor dynamically allocating memory for the second class object & destroying it from the first class destructor.But while debugging control is not going to destructor.Code out line is
class A * Ptr1;
class B;
class A//Class A Declaration
{
public:
A();
B *Ptr2[5];
};
class B()//class B declaration
{public:
int a;
}
A::A()//class A constructor
{
int iI;
for(iI=0;iI<6;iI++)
Ptr2[iI]=new B();//dynamically allocating memory for clas B object
}
B::B()//Class B constructor
{
}
A::~A()//class A destructor
{
delete(Ptr2[5]);//destoying dynamically allocated memory
}
//Main program
int main()
{
Ptr1=new A();//calling class A constructor
}
A's destructor is never being called because you never delete your A object (Ptr1).
Also... you really should avoid dynamically allocating memory like this unless you really need to (which... really... the only time you'd have to is if you have some kind of polymorphism). And even then, it's better to just let smart pointers handle the cleanup.
EDIT:
Also you are deleting your B's incorrectly:
delete(Ptr2[5]);// <- This is wrong
Ptr2[5] is attempting to access (and delete) the 6th element in the Ptr2 array. Since the array only has 5 elements, this is nonsense and will likely cause your program to crash.
It does not delete all elements in the Ptr2 array. To do that, you must delete each element one at a time... just like you new'd them:
1 2
for(int i = 0; i < 5; ++i)
delete Ptr2[i];
ALSO:
You are going out of bounds here:
1 2
for(iI=0;iI<6;iI++) // <- i<6 is wrong. You probably meant i<5 because
Ptr2[iI]=new B(); // Ptr2 only has 5 elements, not 6