#1 2017-09-11 23:13:59

zuoluoq
Member
Registered: 2017-08-15
Posts: 9

TAutoFree clause memory leak in for loop

Hi ab,
It seems like that,I find the memory leak for the Class named TAutoFree.
when I use the TAutoFree.Create(xxx,XXXClass.Create) in a procedure or a function within a loop code,it will be a memory leak reported by FastMM4.
I encapsulated an internal procedure/function within a procedure or a function,and invoke it within a loop,for example,

function TFrmHost.LoadNetworkConfigFileAndConnectToServer: Boolean;
var
  index, retryCount: Integer;
  errorMsg, hintMsg: string;
  encrptionKey, serverAddress, serverPort, wsPort, rootName: string;

  function CheckConnectToServer(): Boolean;
  var
    fClient: TSQLRestClientURI;
    fModel: TSQLModel;
  begin
    TAutoFree.One(fModel, TSQLModel.Create([], rootName));
    TAutoFree.One(fClient, TSQLHttpClient.Create(serverAddress, serverPort, fModel));
    Result := fClient.ServerTimeStampSynchronize;
  end;

begin
  Result := False;
  retryCount := 5;
  for index := 1 to retryCount do
  begin

    Result := CheckConnectToServer;
  
  end;
      // 尝试次数达到最多仍然无法联网
  if not Result then
  begin
    ShowError('无法联网,请检查网络连接或服务器是否正常启用,稍后再试!', '发生错误');
  end;
end;

if I use TAutoFree.Create(fModel, TSQLModel.Create([], rootName)); instead of TAutoFree.One(fModel, TSQLModel.Create([], rootName));
the FastMM4 will give a memory leak report for TAutoFree,but when I use TAutoFree.One(fModel, TSQLModel.Create([], rootName));
it is fine.
Can you tell me the reason ?

Offline

#2 2017-09-12 07:39:10

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

Re: TAutoFree clause memory leak in for loop

One() returns an interface so reference counting works.
Whereas Create() returns a class instance.

Offline

#3 2017-09-14 09:06:50

zuoluoq
Member
Registered: 2017-08-15
Posts: 9

Re: TAutoFree clause memory leak in for loop

thanks.get it.

Offline

Board footer

Powered by FluxBB