Delphi 2010 - Wininet running out of handles
I have an app that makes intensively uses of Wininet
functions to get some data from internet. I am getting a very odd handle related error messages sometimes:
Internal error in ConnectToHost when trying to create a session
ERROR_INTERNET_OUT_OF_HANDLES: No more handles could be generated at this time. Wininet error code = 12001;
When this occured i noticed that my application had more than 500开发者_JS百科0 handles created. I ran a resource profile and I found out that some handles created by wininet
were not being freed.
So, I created a small application to reproduce the issue. The code is simple and does nothing but allocate some wininet
handles and then free them. That is the code:
procedure request(const AUrl : AnsiString);
var
sMethod : AnsiString;
pSession : HINTERNET;
pConnection : HINTERNET;
pRequest : HINTERNET;
port : Integer;
flags : DWord;
begin
pSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(pSession) then
try
Port := INTERNET_DEFAULT_HTTP_PORT;
pConnection := InternetConnectA(pSession, PAnsiChar(AUrl), port, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
if Assigned(pConnection) then
try
sMethod := 'GET';
flags := INTERNET_SERVICE_HTTP;
pRequest := HTTPOpenRequestA(pConnection, PAnsiChar(sMethod), PAnsiChar(AUrl), nil, nil, nil, flags, 0);
try
if Assigned(pRequest) then
ShowMessage('ok');
finally
InternetCloseHandle(pRequest);
end;
finally
InternetCloseHandle(pConnection);
end;
finally
InternetCloseHandle(pSession);
end;
end;
Running this sample on my profiler, I get the same handle related issues.
I think that InternetCloseHandle
is not freeing the handle as it should be because my resource profile tells me that I have 3 live handles when I close the application. Those are the handles that are not being freed:
pRequest
pConnection
pSession
Does anyone know how to get rid of this?
EDIT
The function InternetCloseHandle
is working fine, the return value is true
.
EDIT
I have searched a lot on the internet, but i was not able to find anybody complaining about that. But it is happening. I would like to know if anybody reproduced the issue or if it is just me.
It turned out to be a AQtime problem. I downloaded another profiler and I also took a look at Task Manager and it seems that the handles are being released. But I still get the no more handles
error sometimes and I have no idea why. But I will open another question, since this one was just to see why those handles were not being released.
Thanks for all help I got.
The Http protocol has some limits and the Wininet is using them. Check at WinInet limits connections per server:
WinInet limits connections to a single HTTP 1.0 server to four simultaneous connections. Connections to a single HTTP 1.1 server are limited to two simultaneous connections. The HTTP 1.1 specification (RFC2616) mandates the two-connection limit. The four-connection limit for HTTP 1.0 is a self-imposed restriction that coincides with the standard that is used by a number of popular Web browsers.
Maybe you should wait until connections are closed before attemping a new connection
精彩评论