Memory usage increase continuously when download files using winhttp
    int main()
    {
    	while(true)
    	{
    		Download(_T("192.168.1.1"), _T("myfile.dat"), _T("C:\\downloaded_myfile.dat"));
    		Sleep(1000);
    	}
    
    	return 0;
    }
    
    
    BOOL Download(LPCTSTR lpsz_server_ip, LPCTSTR lpsz_src_file_path, LPCTSTR lpsz_tgt_file_path)
    { 
    	BOOL rc = FALSE;
    	
        DWORD dwReturnHTTPStatusCode;
        
        FILE* fp = NULL;
        
        errno_t err			= 0;
        
       	int iResolveTimeout	= 5000;
    	int iConnectTimeout	= 5000;
    	int iSendTimeout	= 60000;
    	int iReceiveTimeout	= 60000;
     
        HINTERNET  hSession = NULL;
        HINTERNET  hConnect = NULL;
        HINTERNET  hRequest = NULL;
    
    	// 1. create session
        hSession = WinHttpOpen(L"WinHTTP download/1.0", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
    	if(!hSession)
    	{
    		goto __Exit__;
    	}
    
    	// 2. set timeout
    	if(!WinHttpSetTimeouts(hSession, iResolveTimeout, iConnectTimeout, iSendTimeout, iReceiveTimeout))
    	{
    		goto __Exit__;
    	}
    
    	// 3. connect
    	hConnect = WinHttpConnect( hSession, lpsz_server_ip, INTERNET_DEFAULT_HTTP_PORT, 0);
    	if(!hConnect)
    	{
    		goto __Exit__;
    	}
    
    	// 4. open request
    	hRequest = WinHttpOpenRequest( hConnect, L"GET", lpsz_src_file_path,NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
    	if(!hRequest)
    	{
    		goto __Exit__;
    	}
    
    	// 5. send request
    	if(!WinHttpSendRequest( hRequest,WINHTTP_NO_ADDITIONAL_HEADERS, 0,WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
    	{
    		goto __Exit__;
    	}
    
    	// 6. receive response
    	if(!WinHttpReceiveResponse( hRequest, NULL))
    	{
    		goto __Exit__;
    	}
    
    	// 6.1 query header
    	if(!CheckStatus(hRequest, dwReturnHTTPStatusCode))
    	{
    		goto __Exit__;
    	}
    
    	err = _tfopen_s(&fp,lpsz_tgt_file_path,_T("wb"));
    
    	if(0 != err)
    	{
    		goto __Exit__;
    	}
    
        do 
        {
            // 7. check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
    		{
    			goto __Exit__;
    		}
    
    		if(0 == dwSize)
    		{
    			break;
    		}
    
            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize+1];
            if (!pszOutBuffer)
            {
    			goto __Exit__;
            }
            else
            {
                // 8. read the data.
                ZeroMemory(pszOutBuffer, dwSize+1);
    
                if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
    			{
    				delete [] pszOutBuffer;
    				goto __Exit__;
    			}
                else
    			{
    				fwrite(pszOutBuffer,1,dwDownloaded,fp);
    			}
    
                // Free the memory allocated to the buffer.
                delete [] pszOutBuffer;
            }
    
        } while (dwSize>0);
    
    	rc = TRUE;
    
    __Exit__:
    
    	if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
    
    	if(fp) fclose(fp);
    	
    	return rc;
    }
    
    BOOL CheckStatus(HINTERNET &hRequest, DWORD &dwHTTPStatusCode)
    {
    	// 1. check status code
    	WCHAR* lpOutStatusCode		= NULL;
    	DWORD  dwSizeStatusCode		= 0;
        WinHttpQueryHeaders(hRequest,
    						WINHTTP_QUERY_STATUS_CODE,	//WINHTTP_QUERY_STATUS_TEXT or WINHTTP_QUERY_RAW_HEADERS_CRLF
    						WINHTTP_HEADER_NAME_BY_INDEX,
    						NULL, 
    						&dwSizeStatusCode,
    						WINHTTP_NO_HEADER_INDEX);
    
        // Allocate memory for the buffer.
        if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            lpOutStatusCode = new WCHAR[dwSizeStatusCode/sizeof(WCHAR)];
    
            // Now, use HttpQueryInfo to retrieve the header.
            if(!WinHttpQueryHeaders(hRequest, 
    								WINHTTP_QUERY_STATUS_CODE,
                                    WINHTTP_HEADER_NAME_BY_INDEX, 
                                    lpOutStatusCode, 
    								&dwSizeStatusCode, 
                                    WINHTTP_NO_HEADER_INDEX))
    		{
    			delete [] lpOutStatusCode;
    			return FALSE;
    		}
    
    		dwHTTPStatusCode = _wtoi(lpOutStatusCode);
    
    		delete [] lpOutStatusCode;
    
    		if(HTTP_STATUS_OK == dwHTTPStatusCode)
    		{
    			return TRUE;
    		}
        }
    
    	return FALSE;
    }

Memory usage increase continuously when download files using winhttp
===

    int main()
    {
    	while(true)
    	{
    		Download(_T("192.168.1.1"), _T("myfile.dat"), _T("C:\\downloaded_myfile.dat"));
    		Sleep(1000);
    	}
    
    	return 0;
    }
    
    
    BOOL Download(LPCTSTR lpsz_server_ip, LPCTSTR lpsz_src_file_path, LPCTSTR lpsz_tgt_file_path)
    { 
    	BOOL rc = FALSE;
    	
        DWORD dwReturnHTTPStatusCode;
        
        FILE* fp = NULL;
        
        errno_t err			= 0;
        
       	int iResolveTimeout	= 5000;
    	int iConnectTimeout	= 5000;
    	int iSendTimeout	= 60000;
    	int iReceiveTimeout	= 60000;
     
        HINTERNET  hSession = NULL;
        HINTERNET  hConnect = NULL;
        HINTERNET  hRequest = NULL;
    
    	// 1. create session
        hSession = WinHttpOpen(L"WinHTTP download/1.0", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
    	if(!hSession)
    	{
    		goto __Exit__;
    	}
    
    	// 2. set timeout
    	if(!WinHttpSetTimeouts(hSession, iResolveTimeout, iConnectTimeout, iSendTimeout, iReceiveTimeout))
    	{
    		goto __Exit__;
    	}
    
    	// 3. connect
    	hConnect = WinHttpConnect( hSession, lpsz_server_ip, INTERNET_DEFAULT_HTTP_PORT, 0);
    	if(!hConnect)
    	{
    		goto __Exit__;
    	}
    
    	// 4. open request
    	hRequest = WinHttpOpenRequest( hConnect, L"GET", lpsz_src_file_path,NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
    	if(!hRequest)
    	{
    		goto __Exit__;
    	}
    
    	// 5. send request
    	if(!WinHttpSendRequest( hRequest,WINHTTP_NO_ADDITIONAL_HEADERS, 0,WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
    	{
    		goto __Exit__;
    	}
    
    	// 6. receive response
    	if(!WinHttpReceiveResponse( hRequest, NULL))
    	{
    		goto __Exit__;
    	}
    
    	// 6.1 query header
    	if(!CheckStatus(hRequest, dwReturnHTTPStatusCode))
    	{
    		goto __Exit__;
    	}
    
    	err = _tfopen_s(&fp,lpsz_tgt_file_path,_T("wb"));
    
    	if(0 != err)
    	{
    		goto __Exit__;
    	}
    
        do 
        {
            // 7. check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
    		{
    			goto __Exit__;
    		}
    
    		if(0 == dwSize)
    		{
    			break;
    		}
    
            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize+1];
            if (!pszOutBuffer)
            {
    			goto __Exit__;
            }
            else
            {
                // 8. read the data.
                ZeroMemory(pszOutBuffer, dwSize+1);
    
                if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
    			{
    				delete [] pszOutBuffer;
    				goto __Exit__;
    			}
                else
    			{
    				fwrite(pszOutBuffer,1,dwDownloaded,fp);
    			}
    
                // Free the memory allocated to the buffer.
                delete [] pszOutBuffer;
            }
    
        } while (dwSize>0);
    
    	rc = TRUE;
    
    __Exit__:
    
    	if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
    
    	if(fp) fclose(fp);
    	
    	return rc;
    }
    
    BOOL CheckStatus(HINTERNET &hRequest, DWORD &dwHTTPStatusCode)
    {
    	// 1. check status code
    	WCHAR* lpOutStatusCode		= NULL;
    	DWORD  dwSizeStatusCode		= 0;
        WinHttpQueryHeaders(hRequest,
    						WINHTTP_QUERY_STATUS_CODE,	//WINHTTP_QUERY_STATUS_TEXT or WINHTTP_QUERY_RAW_HEADERS_CRLF
    						WINHTTP_HEADER_NAME_BY_INDEX,
    						NULL, 
    						&dwSizeStatusCode,
    						WINHTTP_NO_HEADER_INDEX);
    
        // Allocate memory for the buffer.
        if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            lpOutStatusCode = new WCHAR[dwSizeStatusCode/sizeof(WCHAR)];
    
            // Now, use HttpQueryInfo to retrieve the header.
            if(!WinHttpQueryHeaders(hRequest, 
    								WINHTTP_QUERY_STATUS_CODE,
                                    WINHTTP_HEADER_NAME_BY_INDEX, 
                                    lpOutStatusCode, 
    								&dwSizeStatusCode, 
                                    WINHTTP_NO_HEADER_INDEX))
    		{
    			delete [] lpOutStatusCode;
    			return FALSE;
    		}
    
    		dwHTTPStatusCode = _wtoi(lpOutStatusCode);
    
    		delete [] lpOutStatusCode;
    
    		if(HTTP_STATUS_OK == dwHTTPStatusCode)
    		{
    			return TRUE;
    		}
        }
    
    	return FALSE;
    }
