Home > Back-end >  When using WinHttpApi asynchronous memory leak?
When using WinHttpApi asynchronous memory leak?

Time:09-27

Program I have opened the FastMM, close no hint of any memory leaks
But at the time of execution, a visit to a 5 KB port, about 2000 times the memory is 50 m, very egg pain,
Code:
H_Session,
H_Connect,
H_Request: Pointer;//the unit within the global variable

While true do

The begin
Submit;
Sleep (5);//5 millisecond delay time
The end;

Procedure Submit;
The begin
H_session:=WinHttpOpen (nil, WINHTTP_ACCESS_TYPE_NO_PROXY, nil nil, WINHTTP_FLAG_ASYNC);

WinHttpSetStatusCallback (h_session, AsyncCallBack, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0);

H_connect:=WinHttpConnect (h_session, PChar (' c.tieba.baidu.com '), and urls. The port, 0).

H_request:=WinHttpOpenRequest (h_connect, PChar (' GET '), PChar ('/c/s/filluname), PChar (' HTTP/1.1), nil, nil, 0).

WinHttpSendRequest (h_request, nil, 0, nil, 0, 0, 0).

The end;

The callback function code;
Procedure AsyncCallBack (hInternet: Pointer; DwContext: DWORD; DwInternetStatus:
DWORD. LpvStatusInformation: Pointer; DwStatusInformationLength: DWORD); Stdcall;
Var
Res: TMemoryStream;
Size: DWORD;
Buff: an Array of Char [1.. 4096].
The begin
Case dwInternetStatus of
WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE:
The begin
WinHttpReceiveResponse (hInternet, nil);
The end;
WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE:
The begin
WinHttpSetStatusCallback (hInternet, nil, 0, 0);
Try
Res:=TMemoryStream. Create;
Size:=0;
While WinHttpReadData (hInternet @ Buff, 4096, the Size) do
The begin
If the Size & lt;=0 then Break;
Res. Write (Buff [1], the Size).
Size:=0;
The end;
The finally
If Assigned (hInternet) then WinHttpCloseHandle (hInternet);
FreeAndNil (Res);
The end;
The end;
WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING:
The begin
If Assigned (hInternet) then WinHttpCloseHandle (hInternet);
The end;
The end;
The end;

CodePudding user response:

Background resources will not immediately released, details see below,
http://stackoverflow.com/questions/12652868/winhttpopen-leaking-memory

CodePudding user response:

reference 1st floor pathletboy response:
background resources will not immediately released, details see below,
http://stackoverflow.com/questions/12652868/winhttpopen-leaking-memory


Oh, that is to say, continue to wait for a certain amount of time it will automatically release?

CodePudding user response:

refer to the second floor u013162070 response:
Quote: refer to 1st floor pathletboy response:

Background resources will not immediately released, details see below,
http://stackoverflow.com/questions/12652868/winhttpopen-leaking-memory


Oh, that is to say, continue to wait for a certain amount of time it will automatically release?

Is like this, you can add some long sleep, watching his final release,

CodePudding user response:

reference pathletboy reply: 3/f
Quote: refer to the second floor u013162070 response:

Quote: refer to 1st floor pathletboy response:

Background resources will not immediately released, details see below,
http://stackoverflow.com/questions/12652868/winhttpopen-leaking-memory


Oh, that is to say, continue to wait for a certain amount of time it will automatically release?

That's right, you can add some long sleep, look at the final release, he


SetProcessWorkingSetSize that excuse me, can I use?
  • Related