TIdSocketHandle, bug + fix

Giganews Newsgroups
Subject: TIdSocketHandle, bug + fix
Posted by:  Ben Taylor (to_b…@yahoo.com)
Date: Tue, 12 Apr 2005

Hi,

There's a bug in TIdSocketHandle where the destructor frees a critical
section, then potentially tries to use it. (checked againt latest indy10
nightly).

destructor TIdSocketHandle.Destroy;
begin
  FConnectionHandle.Free;  //frees here, leaving invalid pointer
  CloseSocket;            //potentially uses here
  inherited;
end;

suggested fix:

destructor TIdSocketHandle.Destroy;
begin
  CloseSocket;
  FreeAndNil(FConnectionHandle);
  inherited;
end;

Here's the unit test I use to show it:

procedure TbtIdSocketHandleTest.btTestStackFree;
var
  s:TIdSocketHandle;
begin

  TIdStack.IncUsage;
  s:=TIdSocketHandle.Create(nil);
  try
  s.AllocateSocket;
  finally
  FreeAndNil(s);
  TIdStack.DecUsage;
  end;

end;

Replies