Memory Leak

Hello Wagner; further to my project for creating the API using XData by reading the data from database and pass that through HTTP GET, I've implemented the method to read the image / pdf data stream and send it over using "TXDataOperationContext" very similar to sample GetPdfReport... but at the TMemoryStream I'm creating in "InternalGetMyPdfReport;" function to read the data is not clear. I thought "TXDataOperationContext" do manage the memory at the end.

Please advise.

TXDataOperationContext /* Topics without headers / / Optional user-defined header background / / Breadcrumb Trail */ p#breadcrumbs { font-family: Verdana, Tahoma, Helvetica, Sans-Serif; padding: 2px 0 1px 2px; padding: 0; margin: 0; font-size: 8pt; color: #777777; } p#breadcrumbs a { color: #777777; text-decoration: none;} p#breadcrumbs a:visited { color: #777777; } p#breadcrumbs a:hover { color: #000000; border-bottom: 1px dotted #000000; } HTML { OVERFLOW: hidden } BODY { OVERFLOW: hidden } DIV#hmheader { OVERFLOW: hidden; POSITION: absolute } DIV#idcontent { OVERFLOW-X: auto; OVERFLOW-Y: scroll; POSITION: absolute; VISIBILITY: hidden } #noScriptNavHead { DISPLAY: none } P.autoTOC { COLOR: #444 } .topicmenu UL LI { BACKGROUND-COLOR: #f6f6f6 } DIV#atocIcon { VISIBILITY: visible } .topicmenu { BORDER-TOP-COLOR: #c0c0c0; BORDER-LEFT-COLOR: #c0c0c0; BORDER-BOTTOM-COLOR: #c0c0c0; BORDER-RIGHT-COLOR: #c0c0c0 }

Inspecting request and customizing response

The context also provides you with the request and response objects provided by the TMS Sparkle framework.

You can, for example, set the content-type of a stream binary response:

function TMyService.GetPdfReport: TStream;
begin
TXDataOperationContext.Current.Response.Headers.SetValue('content-type', 'application/pdf');
Result := InternalGetMyPdfReport;
end;

I'm sorry, but your message is really confusing. Could you please provide the code you are using and the report/error you get from your memory leak tool?

function TSDocuments.GetPdfDoc(refid: string; [XDefault('')] tbd: string = ''): TStream;
var
BlobField: TBlobField;
aStream:TMemoryStream;
begin

Result:= TMemoryStream.Create;
TXDataOperationContext.Current.Handler.ManagedObjects.Add(Result);
TXDataOperationContext.Current.Response.Headers.SetValue('content-type', 'application/pdf'); //application/force-download

qry.SQL.Text := 'SELECT access_id, doc_content FROM DOC_R WHERE queued_guid_ref = :rfid';

qry.ParamByName('rfid').AsGUID := FixGUID(refid); // FixGUID will return string
try
qry.Open;
except on E: Exception do
raise EXDataHttpException.Create(400, 'Data not found');
end;
if qry.IsEmpty then
begin
qry.Close;
Result:=VU.AnsiStringToStream(refid);
exit(nil);
end;

with qry do
begin

BlobField:= qry.FieldByName('doc_content') as TBlobField;
if not (BlobField.IsNull) then
begin
  aStream:=TMemoryStream.Create;
  BlobField.SaveToStream(aStream);
  aStream.Position:=0;
  Result.CopyFrom(aStream, aStream.Size);
  aStream.Free;
end;

if active then
  active:=false;

end;

end;

The "GetPdfDoc" method is working well and I can get the PDF file in client side. but when I close the server side application get the memory leak.2020-09-03_10-52-34

I assume the memory leak is because the result stream is still open and its not free yet!

Thanks,
SG.

I don't know what VU is but if this AnsiStringToStream method creates a stream, you are leaking it:

qry.Close;
Result:=VU.AnsiStringToStream(refid);
exit(nil);

Thanks Wagner for speedy response. let me take a look and update you.

VU; is a class that I built for most of the inside utility functions.