Advertisement

07.26.2004 at 03:40PM PDT, ID: 21071996
[x]
Attachment Details

unhandled exception when storing object in CArray.

Asked by simon_holroyd in Microsoft Visual C++.Net

Tags: , ,


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
 
Keywords: unhandled exception when storing obj…
 
Loading Advertisement...
 
[+][-]07.26.2004 at 06:23PM PDT, ID: 11642915

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual C++.Net
Tags: exception, unhandled, delete
Sign Up Now!
Solution Provided By: drichards
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.27.2004 at 02:19AM PDT, ID: 11644595

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.27.2004 at 05:59AM PDT, ID: 11645898

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32