pTable=new struct TableStruct;
nTableCont=GetFile((char*)LPCTSTR(csFileName),pTable);if(!nTableCont)
{
MessageBox("文件不能打开!","错误信息");
FreeMem();
PostMessage(WM_CLOSE);
}
else
{
nTableIndex=0;
while(pTable->pUpTable!=NULL)
    pTable=pTable->pUpTable;nIndexUser=0;
UpdateRecord();
}
}
else语句中出现内存泄露,怎么解决?

解决方案 »

  1.   

    TRY
    {
    ......
    }
    CATCH(CDBException, e)
    {
    .......
    }END_CATCH
    或在
    catch(_com_error &e)
    {
    ...
    }
      

  2.   

    要防止因为异常产生的内存泄漏,可以使用智能指针,也可以用
    __try
    {
    }
    __finally
    {
    }
    《Windows核心编程》一书第23~25章是很好的参考资料。
      

  3.   

    try
    {
    if(!nTableCont)
    {
    MessageBox("文件不能打开!","错误信息");
    FreeMem();
    PostMessage(WM_CLOSE);
    }
    else
    {
    nTableIndex=0;
    while(pTable->pUpTable!=NULL)
        pTable=pTable->pUpTable;nIndexUser=0;
    UpdateRecord();
    }
    }
    catch(_com_error &e)
    {
    }
      

  4.   

    http://www.vchelp.net/wyy/tour/teach_sp_34.htm
      

  5.   

    CFile* pFile = NULL;   // Constructing a CFile object with this override may throw
       // a CFile exception, and won't throw any other exceptions.
       // Calling CString::Format() may throw a CMemoryException,
       // so we have a catch block for such exceptions, too. Any
       // other exception types this function throws will be
       // routed to the calling function.   TRY
       {
          pFile = new CFile(_T("C:\\WINDOWS\\SYSTEM.INI"),
             CFile::modeRead | CFile::shareDenyNone);      DWORD dwLength = pFile->GetLength();      CString str;
          str.Format(_T("Your SYSTEM.INI file is %u bytes long."),
             dwLength);      AfxMessageBox(str);
       }
       CATCH(CFileException, pEx)
       {
          // Simply show an error message to the user.      pEx->ReportError();
       }
       AND_CATCH(CMemoryException, pEx)
       {
          // We can't recover from this memory exception, so we'll
          // just terminate the app without any cleanup. Normally, an
          // an application should do everything it possibly can to
          // clean up properly and _not_ call AfxAbort().      AfxAbort();
       }
       END_CATCH   // If an exception occurs in the CFile constructor,
       // the language will free the memory allocated by new
       // and will not complete the assignment to pFile.
       // Thus, our clean-up code needs to test for NULL.   if (pFile != NULL)
       {
          pFile->Close();
          delete pFile;
       }