Hi all , here is my code for GET request to a http site, but it keeps taking memory nor releasing it, here is the code:

while(1)
{


    hSession = WinHttpOpen(  L"Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0", 
                             WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                             WINHTTP_NO_PROXY_NAME, 
                             WINHTTP_NO_PROXY_BYPASS, 0);


    if (hSession)
        hConnect = WinHttpConnect( hSession, L"lalala.com",
                                   INTERNET_DEFAULT_HTTP_PORT, 0);






	      hRequest = WinHttpOpenRequest( hConnect, L"GET", 
                                       L"/", 
                                       L"HTTP/1.1", WINHTTP_NO_REFERER, 
                                       WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                       0);

bResults = WinHttpSendRequest( hRequest, 
                                    L"Content-Type: application/x-www-form-urlencoded",
                                   -1,
                                    WINHTTP_NO_REQUEST_DATA, 0, 
                                       0, 0);

  if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);



  hRequest = WinHttpOpenRequest( hConnect, L"GET", 
                                       L"/forum/index.php?s=viewforum", 
                                       L"HTTP/1.1", WINHTTP_NO_REFERER, 
                                       WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                       0);

bResults = WinHttpSendRequest( hRequest, 
                                    L"Content-Type: application/x-www-form-urlencoded",
                                   -1,
                                    WINHTTP_NO_REQUEST_DATA, 0, 
                                       0, 0);

  if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);
  

	   if (bResults)
        do 
        {
          
            dwSize = 0;
            if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
              Sleep(1);

           
            pszOutBuffer = new char[dwSize+1];
            if (!pszOutBuffer)
            {
                   dwSize=0;
            }
            else
            {
              
                ZeroMemory(pszOutBuffer, dwSize+1);

                if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                                      dwSize, &dwDownloaded))
                    Sleep(1);
                else
                lol+=pszOutBuffer;

          
                
                delete [] pszOutBuffer;
            }

        } while (dwSize > 0);


  if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);

return 0;

}

I can't figure out whats wrong , I have also checked for memory leak using "crtdbg.h" , but no leaks found , any help will be greatly appreciated.. :)

Dani AI

Generated

Short version: the runaway memory is almost always an unreleased WinHTTP handle or accumulated response data. was right to point at handle closing — every HINTERNET you get must be closed when you’re done with it, and you must never overwrite a handle variable without closing the previous handle first. Also watch any container (your lol string) that keeps appending responses across iterations.

Concrete checklist and fixes:

  • Close each request handle immediately after finishing the response, then close the connect and session handles (close in the reverse order of creation).
  • Check return values from WinHttpOpen/WinHttpConnect/WinHttpOpenRequest/WinHttpReadData and call WinHttpCloseHandle on any successful handles if you bail out early.
  • Append only the number of bytes actually read (dwDownloaded), and avoid allocating a new raw buffer every chunk — reuse a buffer or use a std::vector to simplify lifetime management.
  • Replace tight Sleep(1) loops with proper error handling or a controlled wait; don’t use Sleep to work around failing WinHTTP calls.

Small, safe RAII pattern and read helper to make leaks unlikely:

struct WinHttpHandle {
  HINTERNET h;
  explicit WinHttpHandle(HINTERNET hh = NULL): h(hh) {}
  ~WinHttpHandle(){ if(h) WinHttpCloseHandle(h); }
  void reset(HINTERNET nh){ if(h) WinHttpCloseHandle(h); h = nh; }
  HINTERNET get() const { return h; }
  operator HINTERNET() const { return h; }
};
std::string readAll(HINTERNET req){
  std::string out;
  DWORD size=0, got=0;
  do {
    if(!WinHttpQueryDataAvailable(req, &size)) break;
    if(size==0) break;
    std::vector<char> buf(size);
    if(WinHttpReadData(req, buf.data(), size, &got) && got) out.append(buf.data(), got);
  } while(size>0);
  return out;
}

Debugging tips: CRT leak checks won’t show WinHTTP handle leaks — monitor handle counts with Process Explorer or the debugger, and call GetLastError() when WinHTTP calls fail. After applying the above, the leak should stop; clear or reuse your lol string between iterations if you don’t want it to grow forever.

Recommended Answers

All 3 Replies

you have to call WinHttpCloseHandle () within the loop so that it is closed after each call to WinHttpConnect(). MOve lines 86-88 up above line 83.

you have to call WinHttpCloseHandle () within the loop so that it is closed after each call to WinHttpConnect(). MOve lines 86-88 up above line 83.

Worked!!!! WHOHOO :*

Then i guess you should mark this thread solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.