Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Internet/Controls/TTcpClient    [ Add a report in this area ]  
Report #:  1845   Status: Closed
Resource leak in ttcplient
Project:  Delphi Build #:  6.240
Version:    6.0 Submitted By:   fghd fghdfgh
Report Type:  Minor failure / Design problem Date Reported:  7/18/2002 3:40:48 PM
Severity:    Serious / Highly visible problem Last Updated: 2/22/2005 6:24:31 PM
Platform:    All platforms Internal Tracking #:  
Resolution: Feature Removed (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (3 Total Ratings)
3.67 out of 5
Total Votes: None
Description
There is a rather nasty resource leak in TCustomIpClient
(in sockets.pas) Its Close method calls winsock.shutdown,
but not winsock.closesocket
Without calls to closesocket, the socket resources don't get cleaned up.

This problem was taking down our TTcpServer and TTcpClient based applications after a few thousand calls to Connect/Disconnect, since all sockets are still in use and there no buffer space available for new connections.

Steps to Reproduce:
I ran my program with Sleuth QA 3 and it reported the same resource leaks i suspected.

c := TTcpClient.Create(nil);
c.RemoteHost := host;
c.RemotePort := '80';
c.BlockMode := bmBlocking;
c.Open;
c.SendBuf
...
c.close
c.free

do a netstat -A and see that the socket never gets closed.



---

from sockets.pas:

procedure TCustomIpClient.Close;
begin
  if FConnected then
  begin
{$IFDEF MSWINDOWS}
    ErrorCheck(shutdown(FSocket, SD_BOTH));
{$ENDIF}
{$IFDEF LINUX}
    ErrorCheck(shutdown(FSocket, SHUT_RDWR));
{$ENDIF}
    FConnected := False;
    DoDisconnect;
  end;
  inherited Close;
end;

--
Workarounds
None
Attachment
None
Comments

Shane Miller at 7/19/2002 6:27:29 AM -
I have reported this to Developer support a number of months ago.  I was hoping they would come out with a Update Patch that included this fix because it also caused us to have a very large memory leek.


Andrei Asayonak at 7/19/2002 9:47:09 AM -
Any workarounds?

Stefan Hoffmeister at 7/19/2002 3:13:13 PM -
Looking at the D6.02 code

****************
procedure TBaseSocket.Close;
begin
  if FActive then
  begin



{$IFDEF LINUX}
    ErrorCheck(Libc.__close(FSocket));
{$ENDIF}
    FSocket := INVALID_SOCKET;
    FActive := False;
    DoDestroyHandle;
  end;
end;
****************

it seems as if this is "just" an error in stripping conditionally defined code from the sources. If the blank lines are filled with

{$IFDEF MSWINDOWS}
    ErrorCheck(closesocket(FSocket));
{$ENDIF}

then everything should be fine.

fghd fghdfgh at 7/19/2002 5:06:02 PM -
The provided patch does not seem to work... it still doesnt get closed...

with:

procedure TCustomIpClient.Close;
begin
  if FConnected then
  begin
{$IFDEF MSWINDOWS}
    ErrorCheck(closesocket(FSocket));
{$ENDIF}
{$IFDEF LINUX}
    ErrorCheck(shutdown(FSocket, SHUT_RDWR));
    libc.closesocket(FSocket);
{$ENDIF}
    FConnected := False;
    DoDisconnect;
  end;
  inherited Close;
end;

Stefan Hoffmeister at 7/20/2002 4:05:37 AM -
Which patch do you refer to?

fghd fghdfgh at 7/22/2002 2:26:33 AM -
the patch you provided :)

Stefan Hoffmeister at 7/22/2002 6:14:54 PM -
I did not provide a patch.

I pasted a suggestion which, according to your own comment, you did not implement. Under these circumstances I am not certain how you can complain that "the patch" does not work.

fghd fghdfgh at 7/23/2002 5:16:35 PM -
no, no i am not complaining, i just tried to express in my poor english that meanwhile i tried your suggestion and it does not seem to work :)

Stefan Hoffmeister at 7/24/2002 3:42:07 AM -
Your comment from 19 July clearly shows that you did *not* try my suggestion.

Server Response from: ETNACODE01