I've taken the example TMSWeb_SimpleService.web and added to it a new button. On his OnClick event, I wrote this code:
procedure TForm1.WebButton2Click(Sender: TObject);
var WebRequest :TWebHttpRequest;
begin
WebRequest := TWebHttpRequest.Create(nil);
try
WebRequest.URL := 'http://192.168.10.146:8080/public';
WebRequest.Execute(
procedure(AResponse: string; AReq: TJSXMLHttpRequest)
var
JsonObject :TJSONObject;
TheMessage :string;
begin
WebListBox1.Clear;
try
JsonObject := TJSONObject.ParseJSONValue(AResponse) as TJSONObject;
if Assigned(jsonObject) then begin
TheMessage := jsonObject.GetValue('message').Value;
WebListBox1.Items.Add(TheMessage);
end
else begin
ShowMessage('Error: JSON not valid');
end;
finally
end;
end
);
finally
WebRequest.Free;
end;
end;
It's clear that after the Execute, the program will continue with its execution without waiting for the Response, and the next instruction is WebRequest.Free;
When the response comes from the server, the component does not exist yet.
I've thought that I could put the call to Free inside the execution, but if the program has any problem, the component is going to stay in memory forever.
Or, the model of the memory management of JavaScript has a Garbage collection, and the destruction of this kind of component is not necessary?