【发布时间】: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是空的 -
你为什么要在已经删除的内存上调用
HeapFree?delete和delete[]不要将指针设置为 NULL。所以无论你认为你在这里建立什么“安全”,你都错了。此外,HeapFree应该仅用于通过HeapAlloc获得的内存。