【问题标题】:This may be due to a corruption of the heap error when i run program in MFC 0f c++这可能是由于我在 MFC 0f c++ 中运行程序时堆错误的损坏
【发布时间】:2021-02-16 07:12:47
【问题描述】:

当我运行程序启动时,它会显示一个错误,如图所示。 enter image description here 当我点击继续时,它会显示一个错误,如图所示 enter image description here 这是我写的代码结构。

int nSites = 0;
int nTasks = 0;
int nThreads = 0;
CCriticalSection cs;
BOOL bUpdateList=false;

UINT VisitSite(LPVOID pParam){

    int nTask = 0;
    CInternetSession session;
    session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5000);
    CHttpConnection *pConnection = NULL;
    CHttpFile *pFile1 = NULL;
    char *buffer = new char[10240];
    UINT nBytesRead = 0;
    while(nTasks>0){
    nTask = -1;
    cs.Lock();
    for(int t=0;t<nSites;t++){
        if(!bAssigned[t]){
            bAssigned[t]=true;
            nTask=t;
            t=nSites;
        }
    }
    cs.Unlock();

    if(nTask == -1){
        cs.Lock();
        nThreads--;
        cs.Unlock();
        return 0;
    }
    try
        {
            
            pConnection = session.GetHttpConnection(sAddress[nTask], 80, (_T("")), (_T("")));
            pFile1 = pConnection->OpenRequest((_T("GET")), (_T("/")));
            pFile1->SendRequest();
            nBytesRead = pFile1->Read(buffer, 10240);
            buffer[nBytesRead] = (_T('\0'));
            //ถ้าหากว่า สามารถติดต่อได้ ที่ port 80จริง ให้พิมพ์ข้อความว่า IP Address น้ีเป็น Web server

            if (pConnection != NULL)
            {
                cs.Lock();
                bUpdateList=true;
                bIsWebServer[nTask]=true;
                cs.Unlock();
            }
            cs.Lock();
            bFinished[nTask]=true;
            nTasks--;
            nThreads--;
            cs.Unlock();

            if(pFile1) delete pFile1;
            if(pConnection) delete pConnection;
            delete [] buffer;

            if (buffer != NULL) {
                HeapFree(GetProcessHeap(), 0, buffer);
            }
            if (pFile1 != NULL) {
                HeapFree(GetProcessHeap(), 0, pFile1);
            }
            if (pConnection != NULL) {
                HeapFree(GetProcessHeap(), 0, pConnection);
            }

            //return 0;
        }
        catch (CInternetException* ie )
        {
                
                ie->Delete();
                cs.Lock();
                nTasks--;
                bUpdateList=true;
                bFinished[nTask]=true;
                bIsWebServer[nTask]=false;
                nThreads--;
                cs.Unlock();
        }

    }
    cs.Lock();
    nThreads--;
    cs.Unlock();
    session.Close();
    return 0;
}

因为我认为它来自 *ie 但不确定我是否正确编写了结构,请检查。

【问题讨论】:

  • 我猜pConnection 是空的
  • 你为什么要在已经删除的内存上调用HeapFreedeletedelete[] 不要将指针设置为 NULL。所以无论你认为你在这里建立什么“安全”,你都错了。此外,HeapFree 应该用于通过HeapAlloc 获得的内存。

标签: c++ mfc


【解决方案1】:

看看这个

        char *buffer = new char[10240];
        ...
        delete [] buffer;
        if (buffer != NULL) {
            HeapFree(GetProcessHeap(), 0, buffer);
        }

您不需要使用delete[]HeapFree。因为你使用new[] 分配buffer 你应该使用delete[] 来释放它。移除对HeapFree的调用。

pFile1pConnection 也有同样的问题,使用 delete 不要使用 HeapFree

另一个变化

        if(pFile1) delete pFile1;
        if(pConnection) delete pConnection;

可以简化为

        delete pFile1;
        delete pConnection;

删除 NULL 指针不是错误。删除NULL指针没有任何作用,所以删除前不需要测试指针是否为NULL。

不过,就像 Alan Birtles 所说,您真的应该在尝试使用之前测试 pConnection 是否为 NULL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多