2012-07-12 91 views
0

它写在VC6的功能。堆损坏之后检测从VC6项目升级到VC9

bool CProductionTestDlg::GetVariables(CString strFilename, CMapStringToOb *cVariableMap) 
{ 
    int  iMaxEntryLen = 1000; 
    //char rgbEntryNames[1000];    //previous 
    char *rgbEntryNames = (char*)malloc(iMaxEntryLen * sizeof(int)); //Now 
    CString strEntryName = ""; 
    CString strEntryValue = ""; 
    UINT uiSeperator = 0; 
    ULONG dwRetCode, dwSizeOfReturn; 

    dwSizeOfReturn = GetPrivateProfileString(cszVariables, 
              NULL, 
              "", 
              rgbEntryNames, 
              iMaxEntryLen, 
              strFilename); 

    while (uiSeperator < dwSizeOfReturn) 
    { 
     strEntryName.Format("%s", &rgbEntryNames[uiSeperator]); 
     uiSeperator += strEntryName.GetLength() + 1; 

     CString *strValue = new CString(); 
     dwRetCode = GetPrivateProfileString(cszVariables, 
              strEntryName, 
              "", 
              strEntryValue.GetBufferSetLength(strEntryValue.GetLength()), 
              iMaxEntryLen, 
              strFilename); 
     strValue->Format("%s", strEntryValue);   
     cVariableMap->SetAt(strEntryName, (CObject*)strValue); 

    } 

    return true; 
} 

现在我正确升级它vs08.The项目构建,但是当我打开exe文件抛出异常

* 堆损坏测出* CRT检测到应用程序堆结束后写信给内存缓冲。

当我调试我的应用程序时,控件在返回true后在0123行转到dbgheap.c

+0

我觉得缓冲rgbEntryNames太小 – Jeeva 2012-07-12 05:50:47

+0

好吧我尝试....... – vikky 2012-07-12 05:52:56

+0

我试过了,但问题仍然是一样的...... – vikky 2012-07-12 05:53:37

回答

4

的问题是在这里:

dwRetCode = GetPrivateProfileString(cszVariables, 
    strEntryName, 
    "", 
    strEntryValue.GetBufferSetLength(strEntryValue.GetLength()), 
    iMaxEntryLen, 
    strFilename); 

您传递大小为0(strEntryValue被初始化为"")的缓冲区,但表示,其大小为iMaxEntryLen。因此GetPrivateProfileString认为它具有比实际得到的更大的缓冲区,并且写入超出其范围。

你得到这个错误后,升级的原因,是猜测,边界确认的提高。这个错误在VC6中也存在,只是没有被发现。