Here is the basics of the code:
class MyClass
{
public
CString m_Value1;
CString m_Value2;
};
class MyWrapper
{
private:
MyClass** m_pMyClass;
public:
void Create2dArray(int nHeight, int nWidth);
void Delete2dArray();
};
void MyWrapper::Create2dArray(int nHeight, int nWidth)
{
m_pMyClass = new MyClass*[nHeight];
for (int i = 0; i < nWidth; i++)
m_pMyClass[ i ] = new MyClass[nWidth];
}
void MyWrapper::Delete2dArray()
{
for (int row = 0; row < m_width; row++)
for (int col = 0; col < m_width; col++)
delete &m_pMyClass[row][col];
delete[] m_pMyClass;
}
The problem is my code blows up when I call Delete2dArray() and I don't know why. I have other code that addes values to and references the MyClass objects and all that works fine. It's just this delete that is killing me, any help is much appreciated. The Debug ASsertion that I get says "Expression: _CrtIsValidHeapPointer(pUserData)" and that's really about it. Anyone see what I am doing wrong here

2d array delete problem and question
Tnybubble
Hmm, well, replacing my for loop with that one gets me past deleting those items. But now when I hit the line:
delete[] m_pMyClass
I get the assertion: Damage: after normal block (#62) at 0x004215B0
It seems to me that the for loop is freeing all of the MyClass objects, but if I don't call delete[] m_pMyClass then I have a memory leak.
I'm not sure what I am doing wrong here. Any more help is much appreciated.
Suture
Got it!!!
//free all of the cells
for (int row = 0; row < m_height; row++)
{
MyClass* pPtr = p_mMyClass[row];
if (pPtr)
delete [] pPtr;
}
delete[] p_mMyClass;
anu0987
I'm not an expert on this, but wouldn't this free up the array
void MyWrapper::Delete2dArray()
{
for (int row = 0; row < m_width; row++)
delete[] m_pMyClass[row];
delete[] m_pMyClass;
}
As far as I can remember (but I might be wrong), doing delete[] on each row, will delete also the columns.