tags:

views:

35

answers:

1

hello i am doing a mdi project in visual c++ using mfc i am using a CArray to save and load data to documents. CArray contains members of a custom class of mine.

Saving and loading works fine

I get access violation when closing a document that i have opened, and debugger points me to carray.

the weird thing is that i can close without problem "new" documents and the program crashes only when i am trying to close a document that i "open".

any idea how to fix it?

A: 

debugger points me to that code

i got the message "TYPE not found"

CArray::~CArray() { ASSERT_VALID(this);

  if (m_pData != NULL)
  {
      for( int i = 0; i < m_nSize; i++ )
          (m_pData + i)->~TYPE();
      delete[] (BYTE*)m_pData;
  }
}

what code u want to see exactly?

forgot to mention that if i open an empty document closing is working fine as well

joe
Why are you explicitly calling the destructor, then deleting it as BYTE-pointer? I suppose that the delete of the BYTE-pointer does not execute the destructor again? If it does this, your destructor is executed twice and this can explain your crash (you should verify this). A better way of writing this is just to "delete[] m_pData". This will call the destructor of all the instances in your array, and free the memory.
Patrick
that code is not mine is the carray destructor in afxtempl.h.i post it to show where the problem occurs
joe
That posted code is fine: this is the use of an in-place destructor to call the destructor for TYPE, then the delete[] cleans up the memory. This allows the CArray object to manage the memory for the underlying array as one block.
DavidK
At which line in this does the debugger point you? The assertion, or something below it?
DavidK