[英]Delphi XE + thread + idHttp without memory leaks
At first HI ALL and sry for my English. 起初HI ALL和我的英语。 can somebody share work source with create + destroy threads with simple GET in execute ?
有人可以在执行时使用简单的GET与create + destroy线程共享工作源吗?
i try do it by myself but always get memory leaks(( 我尝试自己做,但总是得到内存泄漏((
i test it with code at end of source 我在源代码处用代码测试它
initialization
ReportMemoryLeaksOnShutdown := True;
btw ill Google it 2 week and test many samples... and always have leaks by default =( btw生病谷歌它2周,并测试了许多样品......并且默认情况下总是有泄漏=(
delphi XE7 32bit at windows 7 x64 windows 7 x64上的delphi XE7 32位
when i press stop button i still see some connections 当我按下停止按钮时,我仍然看到一些连接
after closing i get this message 关闭后,我收到此消息
cant post image, need 10 reputation... 无法发布图片,需要10个声望......
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, sButton, sMemo, sEdit,
sSpinEdit, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,System.SyncObjs;
type
TForm1 = class(TForm)
StartBtn: TsButton;
StopBtn: TsButton;
ThreadCount: TsSpinEdit;
sdt1: TsEdit;
sm1: TsMemo;
procedure StartBtnClick(Sender: TObject);
procedure StopBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Thread = class(TThread)
private
HTTP : TIdHTTP;
result:integer;
InputIndex:integer;
public
procedure Local;
constructor Create(CreateSuspended:boolean);
destructor Destroy; override;
protected
procedure Execute; override;
end;
var
Form1: TForm1;
LocalWork: Boolean;
target: string;
implementation
{$R *.dfm}
constructor Thread.Create(CreateSuspended: boolean);
begin
Inherited Create(true);
FreeOnTerminate:=true;
HTTP:=TIdHTTP.Create(nil);
HTTP.ReadTimeout := 2000;
Resume;
end;
destructor Thread.Destroy;
begin
try
If HTTP.Connected then
begin
HTTP.Disconnect(false);
HTTP.IOHandler.InputBuffer.Clear();
HTTP.IOHandler.Close;
Terminate;
end;
finally
WaitFor;
FreeAndNil(HTTP);
end;
inherited;
end;
procedure Thread.Execute;
begin
while (LocalWork=True) do
begin
if LocalWork=true then
begin
HTTP.Get(target);
if HTTP.ResponseCode=200 then
begin
result:=1;
end
else
begin
result:=2;
end;
Synchronize(Local);
end
else
begin
EndThread(0);
end;
end;
EndThread(0);
end;
procedure Thread.Local;
begin
if result=1 then Form1.sm1.Lines.Add('Good ');
if result=2 then Form1.sm1.Lines.Add('Bad ');
end;
procedure TForm1.StartBtnClick(Sender: TObject);
var
i:integer;
begin
target := sdt1.Text;
LocalWork := True;
for I := 0 to ThreadCount.Value-1 do
begin
sm1.Lines.Add('Thread createrd '+inttostr(i));
Thread.Create(true); // создаем замароженный поток
end;
end;
procedure TForm1.StopBtnClick(Sender: TObject);
begin
LocalWork:=false;
end;
initialization
ReportMemoryLeaksOnShutdown := True;
end.
inherited Create(false);
inherited Create(false);
. Resume
call at the end. Resume
调用。 The thread will not start until the constructor has finished anyway. Execute
method, skip the EndThread
calls, since the thread will handle this when the Execute
method ends. Execute
方法中,跳过EndThread
调用,因为线程将在Execute
方法结束时处理此问题。 Destroy
destructor, do not call Terminate
and Waitfor
. Destroy
析构函数中,不要调用Terminate
和Waitfor
。 They do not belong there at all. FreeOnTerminate
, and will do so gracefully. FreeOnTerminate
,并将优雅地执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.