tags:

views:

106

answers:

1

I am using TWebModule with Apache. I am having problems with a memory leak. In the code below is not freeing the ImageStream a memory leak? If I do free 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;
+2  A: 

From here:

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

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

Response.ContentStream := TMemoryStream.Create

and save the image to that stream directly?

JimG
Thanks - I kind of expected that to be the answer. And I will employ your idea too. My memory leak must be somewhere else!
M Schenkel