Doesn't release TMemoryStream causing a memory leak?

I am using TWebModule with Apache. I have a memory leak problem. Doesn't the ImageStream memory leak free in the code below ? If I release it, I get an access violation.

procedure TWebModule1.WebModule1WebActionItem8Action(Sender: TObject;
    Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  var
    RecNo: Integer;
    ImageStream: TmemoryStream;
  begin
    RecNo := StrToInt(Request.QueryFields.Values['RecNo']);
    Master.MoveBy(RecNo - Master.RecNo); // go to right record
    ImageStream := TMemoryStream.Create;
    with TGraphicField.Create(Master) do
    try
      FieldName := 'Graphic';
      SaveToStream(ImageStream)
    finally
      Free
    end;
    ImageStream.Position := 0; // reset ImageStream
    Response.ContentType := 'image/jpg';
    Response.ContentStream := ImageStream;
    Response.SendResponse
  end;
+3
source share
1 answer

From here :

If you use the ContentStream property, do not free the stream yourself. The response web object automatically releases it for you.

Having said that, why use ImageStream at all? Why not just use:

Response.ContentStream := TMemoryStream.Create

and save the image in this stream directly?

+4
source

Source: https://habr.com/ru/post/1759667/


All Articles