prem
2004-01-31 09:06:07 UTC
Hello Everybody,
I am developing a HTTP based application in which I am using WinHttp5 extensively. I noticed that machine becomes slow after the program is installed. I tested the program using Rational Purify to check if there are any memory leaks in the program. I tried a small test code ( I have attached the code in this mail ) and tested using Purify. Purify pointed out to the line containing WinHttpSendRequest() for memory leaks. I tried different combinations but still I see purify pointing to that line for memory leaks.
Is there any problem in my piece of code (or) is the winHttp library is responsible for memory leak ?
If WinHttp is the problem point, is there any way to overcome it ?
Thanks in advance.
-Prem
HINTERNET hPeer, hConnect, hReq;
WCHAR wcharHost[MAX_PATH];
DWORD dwError=0;
char sBuffer[MAX_PATH];
DWORD dwBytesRead=0;
DWORD dwFlags = WINHTTP_FLAG_REFRESH | WINHTTP_FLAG_SECURE;
INTERNET_PORT PORT = INTERNET_DEFAULT_HTTPS_PORT;
WCHAR *pszHeader = NULL;
char *pszPostParam = NULL;
wcscpy(wcharHost,L"192.168.1.1");
if ( !(hPeer = WinHttpOpen ( L"test", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0) ) )
{
return;
}
if (!(hConnect = WinHttpConnect( hPeer,(LPCWSTR)wcharHost,PORT,0)))
{
WinHttpCloseHandle(hPeer);
return;
}
if ( !(hReq = WinHttpOpenRequest (hConnect,L"POST", L"/abc/servletname", NULL, NULL, NULL, dwFlags) ))
{
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hPeer);
return;
}
dwError=0;
pszHeader = new WCHAR[MAX_PATH];
pszPostParam = new char[MAX_PATH];
wcscpy(pszHeader,L"Content-Type: application/x-www-form-urlencoded");
strcpy(pszPostParam, "hostname=192.168.1.1&version=1.0");
if ( !WinHttpSendRequest(hReq, pszHeader, wcslen(pszHeader), (LPVOID)pszPostParam, strlen(pszPostParam),strlen(pszPostParam),0 ))
dwError = GetLastError ();
// End the request.
bRet = WinHttpReceiveResponse( hReq, NULL);
memset(sBuffer, 0, MAX_PATH);
WinHttpReadData(hReq, sBuffer, MAX_PATH*sizeof(char), &dwBytesRead );
sBuffer[dwBytesRead]=0;
WinHttpCloseHandle(hReq);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hPeer);
delete [] pszHeader;
delete [] pszPostParam;
I am developing a HTTP based application in which I am using WinHttp5 extensively. I noticed that machine becomes slow after the program is installed. I tested the program using Rational Purify to check if there are any memory leaks in the program. I tried a small test code ( I have attached the code in this mail ) and tested using Purify. Purify pointed out to the line containing WinHttpSendRequest() for memory leaks. I tried different combinations but still I see purify pointing to that line for memory leaks.
Is there any problem in my piece of code (or) is the winHttp library is responsible for memory leak ?
If WinHttp is the problem point, is there any way to overcome it ?
Thanks in advance.
-Prem
HINTERNET hPeer, hConnect, hReq;
WCHAR wcharHost[MAX_PATH];
DWORD dwError=0;
char sBuffer[MAX_PATH];
DWORD dwBytesRead=0;
DWORD dwFlags = WINHTTP_FLAG_REFRESH | WINHTTP_FLAG_SECURE;
INTERNET_PORT PORT = INTERNET_DEFAULT_HTTPS_PORT;
WCHAR *pszHeader = NULL;
char *pszPostParam = NULL;
wcscpy(wcharHost,L"192.168.1.1");
if ( !(hPeer = WinHttpOpen ( L"test", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0) ) )
{
return;
}
if (!(hConnect = WinHttpConnect( hPeer,(LPCWSTR)wcharHost,PORT,0)))
{
WinHttpCloseHandle(hPeer);
return;
}
if ( !(hReq = WinHttpOpenRequest (hConnect,L"POST", L"/abc/servletname", NULL, NULL, NULL, dwFlags) ))
{
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hPeer);
return;
}
dwError=0;
pszHeader = new WCHAR[MAX_PATH];
pszPostParam = new char[MAX_PATH];
wcscpy(pszHeader,L"Content-Type: application/x-www-form-urlencoded");
strcpy(pszPostParam, "hostname=192.168.1.1&version=1.0");
if ( !WinHttpSendRequest(hReq, pszHeader, wcslen(pszHeader), (LPVOID)pszPostParam, strlen(pszPostParam),strlen(pszPostParam),0 ))
dwError = GetLastError ();
// End the request.
bRet = WinHttpReceiveResponse( hReq, NULL);
memset(sBuffer, 0, MAX_PATH);
WinHttpReadData(hReq, sBuffer, MAX_PATH*sizeof(char), &dwBytesRead );
sBuffer[dwBytesRead]=0;
WinHttpCloseHandle(hReq);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hPeer);
delete [] pszHeader;
delete [] pszPostParam;