我正在尝试使用线程进行 Web 服务调用。出于这个原因,我定义了一个简单的对象并从线程中调用它的方法。
但我意识到,在销毁该对象后,我仍然可以使用它的方法而不会出现任何错误,并且它会返回正确的答案。怎么可能?你有什么意见吗?
// TThreadMethod is defined in Classes.pas
TThreadMethod = procedure of object;
*********************
Interface
*********************
TMyObject = class(TObject)
procedure DoSomething;
end;
TMyThread = class(TThread)
private
FMethod : TThreadMethod;
protected
procedure Execute; override;
public
constructor Create(aMethod: TThreadMethod);
end;
*********************
Implementation
*********************
// I called the thread and destroyed the object immediately.
procedure TForm1.Button1Click(Sender: TObject);
var
aObject : TMyObject;
begin
aObject := TMyObject.Create;
TMyThread.Create(aObject.DoSomething);
FreeAndNil(aObject); // Object itself was destroyed here.
end;
constructor TMyThread.Create(aMethod: TThreadMethod);
begin
inherited Create(True);
FreeOnTerminate := True;
FMethod := aMethod;
Resume;
end;
procedure TMyThread.Execute;
begin
Sleep(10000); // wait 10 sec. before calling the method
// The object was already destroyed but it doesn't produce any error!
FMethod; // It calls the web service method and returns correct result. But how???
end;
procedure TMyObject.DoSomething;
begin
// Call a web service method and show the result on the form.
end;