#1 2022-07-04 22:24:39

tbo
Member
Registered: 2015-04-20
Posts: 353

Memory Leak with ReturnFile?

Delphi 11.1, mORMot 3547

I have a method based services with several functions. They all work fine, only this function creates a memory leak.

if (pmCtxt.Method = mGET)
  and GetSessionUserDirName(pmCtxt, dirName)
  and UrlDecodeNeedParameters(pmCtxt.Parameters, TFileServiceFunction.Param.LoadFile_ImageName) then
begin
  Utf8ToFileName(pmCtxt.InputUtf8[TFileServiceFunction.Param.LoadFile_ImageName], imageName);
  if ExtractFileExt(imageName) = '' then
    imageName := ChangeFileExt(imageName, DEFAULT_FILE_EXT);

  fileName := MakePath([DataFolder, dirName, imageName]);
  if FileExists(fileName) then
  begin
    pmCtxt.ReturnFile(fileName);
    Exit; //=>
  end;
end;

When I run this function once, I get this message when closing the server:

Press [Enter] to quit server...
Unexpected Memory Leak
An unexpected memory leak has occurred. The unexpected small block leaks are:
13 - 20 bytes: TFileStream x 1
125 - 132 bytes: UnicodeString x 1

When I run this function three times, I get this message:

Press [Enter] to quit server...
Unexpected Memory Leak
An unexpected memory leak has occurred. The unexpected small block leaks are:
13 - 20 bytes: TFileStream x 3
141 - 148 bytes: UnicodeString x 3

The function ReturnFile() is called only here, other functions also elsewhere.
What could be the reason?

With best regards
Thomas

Offline

#2 2022-07-05 11:08:13

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: Memory Leak with ReturnFile?

I use the Http server (useHttpSocket). In the function THttpRequestContext.ContentFromFile() the FileStream is created but not released. Also the function THttpRequestContext.ProcessDone is not called. Thus, there is no point at which the FileStream is released again. I think this change came with the Async server.

So, a FileStream is created which is not destroyed anymore. This creates the memory leak.

With best regards
Thomas

Last edited by tbo (2022-07-05 11:12:29)

Offline

#3 2022-07-05 22:01:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,657
Website

Re: Memory Leak with ReturnFile?

Offline

Board footer

Powered by FluxBB