My class has a generic array of pointers member named A:
T** A
Currently, i delete the array in the destructor in the following way:
~MyQuickInitArray(){
delete [] A;
};
Will this cause a memory leak? if so, should i iterate through the array and call delete on each object?
On a side note – Do I need to call delete [] B if B is an array of integers or does the destructor handles it already?
EDIT:
This is how the allocation occurs:
MyQuickInitArray(int size)
{
if(size <= 0)
{
throw new std::exception;
}
_size = size;
_counter = 0;
A = new T*[size];
B = new int[size];
C = new int[size];
}
MyQuickInitArray(const MyQuickInitArray& myQuickInitArray)
{
_size = myQuickInitArray._size;
_counter = myQuickInitArray._counter;
A = new T*[_size];
for(int i = 0; i<_size ;i++)
{
if(myQuickInitArray.A[i] != NULL)
{
A[i] = new T(*myQuickInitArray.A[i]);
}
}
B = myQuickInitArray.B;
C = myQuickInitArray.C;
}
You can surely count on it if
Ais an array of pointers. You need loop through the array and delete the pointers yourself.Yes, always delete that which is allocated with
new. You can do this inside your class’s destructor.