我有一个程序,当我单击“下载”按钮时,程序会创建一个新线程来下载网页并将其存储在动态分配char*
变量中。
但现在我点击“下载”,程序显示以下信息:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: d:\dev\debug\test.exe
File: dbgheap.c
Line: 1279
Expression: _CrtIsValidHeapPointer(pUserData)
似乎问题与堆分配或释放问题有关。
void SomeClass::get()
{
buf = this->download(url);
while (some condition)
{
......
......
bufContent = this->download(url);
......
......
sql = new char[sqlSize];
ZeroMemory(sql,sqlSize);
sql_utf8 = new char[sqlSize*2];
ZeroMemory(sql_utf8,sqlSize*2);
......
......
delete[] bufContent;bufContent=NULL;
delete[] sql;
delete[] sql_utf8;
}
delete[] buf; buf=NULL;//debug run to here, get Assertion Failed error
}
download
功能 :
char* SomeClass::download(TCHAR* url)
{
char * pBuf = NULL ;
int nBufLen = 0 ;
TRY
{
// connection
CInternetSession sess ;
sess.SetOption (INTERNET_OPTION_CONNECT_TIMEOUT, 30 * 1000) ;
sess.SetOption (INTERNET_OPTION_CONNECT_BACKOFF, 1000) ;
sess.SetOption (INTERNET_OPTION_CONNECT_RETRIES, 1) ;
DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD ;
CHttpFile * pF = (CHttpFile*)sess.OpenURL(url, 1, dwFlag); ASSERT(pF);
if (!pF)
{AfxThrowInternetException(1);}
// connection status
CString str ;
pF->QueryInfo (HTTP_QUERY_STATUS_CODE, str) ;
if (str != _T("200"))
{
pF->Close() ;
delete pF ;
AfxThrowInternetException(1);
}
// start QzoneBlog
int nLen,nLenCopy;
pF->QueryInfo (HTTP_QUERY_CONTENT_LENGTH, str) ; // file's length
if (_ttoi(str))
{
// know file's size
nLenCopy = nLen = (nBufLen = _ttoi(str)) ;
char * p = (pBuf = new char[nLen+8]) ;
ZeroMemory (p, nLen+8) ;
while (TRUE)
{
int n = pF->Read (p, (nLen < 1024) ? nLen : 1024) ;
if (n <= 0)
break ; // success exit
p += n ; nLen -= n ;
}
// interrupted
if (nLen != 0)
{
//delete[] pBuf; pBuf=NULL;
nBufLen = 0 ;
}
}
pF->Close() ;
delete pF ;
return pBuf;
}
CATCH_ALL(e) {
return 0;
}
END_CATCH_ALL
}