Delphi Datasnap Server Memory Leak using TFileStre

2019-03-06 02:02发布

i need a function in a datasnap-server, which returns a .zip file. So i started with:

function TGetData.getZip (): TFileStream;
begin
   result := TFileStream.Create('test.zip', fmOpenRead and fmShareDenyWrite);
end

This works fine, but datasnap doesn't free it, so i get a memory leak error.

Next try: I started at "TWebModule1.WebModuleAfterDispatch". I thought it could help to send my response with "response.SendResponse;" and free my stream on my own. So here a short version:

procedure TWebModule1.WebModuleAfterDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
   test : TFileStream;
begin
   response.ContentType := 'application/x-zip-compressed';
   test := TFileStream.Create('test.zip', fmOpenRead);
   response.ContentStream := test;
   response.CustomHeaders.Values['Content-Disposition'] := 'attachment; filename=test12.zip';
   response.SendResponse; //Also sendStream didn't help
   test.Free;
end;

This is nearly the solution i think, but the datasnap-server sends html-code after my stream was sent and this is written at the end of the file.

I know, the datasnap-server doenst support TFileStream. Instead i should use TDBXStreamValue, but there seems to be no working example...

Has any one expirience with such a problem?

1条回答
Evening l夕情丶
2楼-- · 2019-03-06 02:24

Thank you @remy lebeau

response.FreeContentStream := true;

this is the solution.

My function:

function TGetData.articleZippedImages(skip, take: Integer; since: string) : TStream;

the function does not free the stream!

procedure TWebModule1.WebModuleAfterDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
   response.FreeContentStream := true;
   Response.Content := '';
   Response.CustomHeaders.Values['Content-Disposition'] := 'attachment; filename=test.zip';
   Response.SendResponse;
end;
查看更多
登录 后发表回答