void fun()
{
    CMemFile mf; 
    CArchive ar(mf, CArchive::store); //mf用内部缓冲
    
    // ... some operation
}fun()函数是工程里一个调用很频繁的函数(被定时器调用),经常异常退出,我看了,是在ar析构时调用Flush()时出错,出错语句用红色语句标出 如下
CArchive::~CArchive()
{
// Close makes m_pFile NULL. If it is not NULL, we must Close the CArchive
if (m_pFile != NULL && !(m_nMode & bNoFlushOnDelete))
Close(); Abort();    // abort completely shuts down the archive
}void CArchive::Close()
{
ASSERT_VALID(m_pFile); Flush();
m_pFile = NULL;
}void CArchive::Flush()
{
ASSERT_VALID(m_pFile);
ASSERT(m_bDirectBuffer || m_lpBufStart != NULL);
ASSERT(m_bDirectBuffer || m_lpBufCur != NULL);
ASSERT(m_lpBufStart == NULL ||
AfxIsValidAddress(m_lpBufStart, m_lpBufMax - m_lpBufStart, IsStoring()));--------出错处!!!!!
ASSERT(m_lpBufCur == NULL ||
AfxIsValidAddress(m_lpBufCur, m_lpBufMax - m_lpBufCur, IsStoring())); if (IsLoading())
{
// unget the characters in the buffer, seek back unused amount
if (m_lpBufMax != m_lpBufCur)
m_pFile->Seek(-(m_lpBufMax - m_lpBufCur), CFile::current);
m_lpBufCur = m_lpBufMax;    // empty
}
else
{
if (!m_bDirectBuffer)

// write out the current buffer to file
if (m_lpBufCur != m_lpBufStart)
m_pFile->Write(m_lpBufStart, m_lpBufCur - m_lpBufStart);
}
else
{
// commit current buffer
if (m_lpBufCur != m_lpBufStart)
m_pFile->GetBufferPtr(CFile::bufferCommit, m_lpBufCur - m_lpBufStart);
// get next buffer
VERIFY(m_pFile->GetBufferPtr(CFile::bufferWrite, m_nBufSize,
(void**)&m_lpBufStart, (void**)&m_lpBufMax) == (UINT)m_nBufSize);
ASSERT((UINT)m_nBufSize == (UINT)(m_lpBufMax - m_lpBufStart));
}
m_lpBufCur = m_lpBufStart;
}
}
          
大概是说地址非法 被这个问题困扰多时,请兄弟姐妹指点 ,50分送上 在线等!!
   

解决方案 »

  1.   

    怀疑AfxIsValidAddress第3个参数不对,那个IsStoring()函数你怎么返回的?BOOL   AFXAPI   AfxIsValidAddress(const   void*   lp,   UINT   nBytes, 
    BOOL   bReadWrite   /*   =   TRUE   */) 

    //   simple   version   using   Win-32   APIs   for   pointer   validation. 
    return   (lp   !=   NULL   &&   !IsBadReadPtr(lp,   nBytes)   && 
    (!bReadWrite   ||   !IsBadWritePtr((LPVOID)lp,   nBytes))); 
    }