Memory Leak with TStream result

Hello,

I made a simple service like this:

function TMyService.AllReport: TStream;
begin
	Result := TStringStream.Create('{"name":"Hans","Address":[{"new":"98st"},{"old":"123st"}]}');
    TXDataOperationContext.Current.Handler.ManagedObjects.Add(Result);
end;

then the client accesses with a code like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  Client: TXDataClient;
  ms: TMemoryStream;
  sResp: string;
begin
  Client := TXDataClient.Create;
  ms := TMemoryStream.Create;
  try
    Client.Uri := 'http://localhost:2001/guest';
    ms.LoadFromStream(Client.Service<IMyService>.AllReport);
    SetString(sResp, PAnsiChar(ms.Memory), ms.Size);
    Memo1.Text := sResp;
  finally
    Client.Free;
    ms.Free;
  end;
end;

on the client there will be a TBytestream memory leak.
Please help, which part of the code is wrong

thank you

This is the correct client code:

procedure TForm1.Button1Click(Sender: TObject);
var
  Client: TXDataClient;
  ms: TMemoryStream;
  stream: TStream;
  sResp: string;
begin
  Client := TXDataClient.Create;
  ms := TMemoryStream.Create;
  try
    Client.Uri := 'http://localhost:2001/guest';
    stream := Client.Service<IMyService>.AllReport;
    try
      ms.LoadFromStream();
    finally
      stream.Free;
    end;
    SetString(sResp, PAnsiChar(ms.Memory), ms.Size);
    Memo1.Text := sResp;
  finally
    Client.Free;
    ms.Free;
  end;
end;

More info:
https://download.tmssoftware.com/business/xdata/doc/web/client_memory_management.html

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.