Indy memory leaks

Giganews Newsgroups
Subject: Indy memory leaks
Posted by:  Rob Roberts (robrRemoveTh…@AndThisToo.pcisys.net)
Date: Sat, 30 Aug 2003

Indy v9.0.14 has the following memory leaks:

IdHTTPHeaderInfo.pas:

In TIdRequestHeaderInfo.SetHeaders a TIdBasicAuthentication instance is
created but never destroyed:

Authentication := TIdBasicAuthentication.Create;

The fix is to add a destructor to TIdRequestHeaderInfo, and then free the
instance within the destructor:

declaration:

destructor Destroy; override;

implementation:

destructor TIdRequestHeaderInfo.Destroy;
begin
  FreeAndNil(Authentication);
  inherited;
end;

IdSSLOpenSSL.pas:

In TIdSSLSocket.GetPeerCert a TIdX509 instance is created by never
destroyed:

fPeerCert := TIdX509.Create(X509);

The fix is to free the instance within TIdSSLSocket.Destroy:

destructor TIdSSLSocket.Destroy;
begin
  if fSSL <> nil then begin
    //IdSslSetShutdown(fSSL, OPENSSL_SSL_SENT_SHUTDOWN);
    IdSslShutdown(fSSL);
    IdSslFree(fSSL);
    fSSL := nil;
  end;
  if fSSLCipher <> nil then begin
    FreeAndNil(fSSLCipher);
  end;
  FreeAndNil(fPeerCert); // <---- Fixes memory leak
  inherited Destroy;
end;

Also, the check for fSSLCipher <> nil in the above destructor is of course
unneccessary.  This block of code:

  if fSSLCipher <> nil then begin
    FreeAndNil(fSSLCipher);
  end;

Can be simplified to this:
  FreeAndNil(fSSLCipher);

--Rob Roberts

Replies