Raksit kongwong
Raksit kongwong

Reputation: 1

This may be due to a corruption of the heap error when i run program in MFC 0f c++

When i run program start it will show an Error as in the picture. enter image description here and When i clikced continue it will show an Error as in the picture enter image description here This is the code structure I wrote.

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;
}

Cause i think it comes from *ie but Not sure if I wrote the structure correctly, please check.

Upvotes: 0

Views: 75

Answers (1)

john
john

Reputation: 87957

Look at this

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

You don't need to use delete[] and HeapFree. Because you used new[] to allocate buffer you should use delete[] to free it. Remove the call to HeapFree.

You have the same problem with pFile1 and pConnection, use delete don't use HeapFree.

Another change

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

can be simplified to

        delete pFile1;
        delete pConnection;

It's not an error to delete a NULL pointer. Deleteing a NULL pointer has no effect, so you don't need to test if a pointer is NULL before deleting it.

However like Alan Birtles said you really should test if pConnection is NULL before you try to use it.

Upvotes: 3

Related Questions