DELPHI XE 内存泄漏 An unexpected memory leak has occured

function TfrmMainForm.GetSelectedConnection: TIdTCPConnection;
var
    PairEnum: TObjectDictionary<string,TIdTCPConnection>.TPairEnumerator;
    Conn: TIdTCPConnection;
    IPS:STRING;
begin
    //从列表中选择,返回    TIdTCPConnection;为后续的关闭做准备

  ReportMemoryLeaksOnShutdown := True;//开启内存泄漏报告模式
    try
       if  ServerContainer1.FTCPConnection  =nil   then
       begin
         showmessage('ServerContainer1.FTCPConnectio =nil');
       end;
      PairEnum := ServerContainer1.FTCPConnection.GetEnumerator;
      while PairEnum.MoveNext do
       begin
          ips := PairEnum.Current.Key;
          if  L_UserLink.Selected=nil then
              exit;
          if L_UserLink.Selected.SubItems[0]=ips then
            result:=PairEnum.Current.Value;
       end;
    finally
      PairEnum.CleanupInstance; 

一般人没有加上面这行代码所以会报错 An unexpected memory leak has occured. the unexpected smal block                       leaks are

解决办法是在FREE之前加  CleanupInstance; 

} 
      PairEnum.Free;
    end;

end;
 


猜你喜欢

转载自blog.csdn.net/qq_25439957/article/details/88635643