Hello,
Here's a brief overview of my code, I hope it makes sense, it's not very complicated.
class CTest
{
public:
CTest(void);
virtual ~CTest(void);
CString *pLabel;
CString getName(int index);
};
//CTest class definitions
CTest::CTest(void)
: pLabel(NULL)
{
this->pLabel = new CString[5];
this->pLabel[0] = "Simon";
this->pLabel[1] = "Nancy";
this->pLabel[2] = "Mark";
this->pLabel[3] = "Trent";
this->pLabel[4] = "Anna";
}
CTest::~CTest(void)
{
TRACE("Executing CTest destructor now.\n");
//if(this->label != NULL)
delete [] this->pLabel;
}
CString CTest::getName(int index)
{
VERIFY(index < 5 && index >= 0);
return this->pLabel[index];
}
That's the nonstandard code out of the way now onto the standard dialog classes.
I create the following var to store instances of the CTest object in CTestAreaDlg:
CArray<CTest,CTest&> m_test_store;
I then set the initial size of the CArray in OnInitDialog:
m_test_store.SetSize(0,1);
I then have a button that adds the default CTest object to the CArray via the standard call back. The problem is that when I cancel the dialog I get the following error:
Unhandled exception at 0x77f75a58 in TestArea.exe: User breakpoint.
I delete the CString array in ~CTest:
delete [] this->pLabel;
The debugger takes me to the dbgheap.c _CrtIsValidHeapPointer method. I think that I understand that the CArray calls the CTest destructor when the CTest object is added to the CArray after copying the contents to the internal object, is that right? Does anyone know what is causing this error? It seems to be trying to execute the destructor of the same object twice. The first time is fine as there is someing to delete, but the second time the pointer is null.
Help!
Start Free Trial