Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Jan : TIdTCPClient - Disconnection error on DLL unloading

www.cryer.info
Managed Newsgroup Archive

TIdTCPClient - Disconnection error on DLL unloading

Subject:TIdTCPClient - Disconnection error on DLL unloading
Posted by:"Adrien Reboisson" (adrien-reboissonatastase.c..@polak.com)
Date:12 Jan 2006 05:30:15

Hi,

I have built a DLL (using D7) which contains a list of sockets
(TIdTCPClient). The list, which inherits from TObjectList, is
created in the initialization section of an unit included by the
project file, and symetrically destroyed in it finalization
section.

Applications which use the DLL call exported functions. They
create sockets which are nor destroyed and disconnected when the
functions ends but rather injected in the TObjectList in order to be "reused" if possible the next time a function which needs to
dialog with the same host is called. So I avoid to connect/disconnect systematically sockets when a function is
called, which improves the speed of the process.


In theory, when the client app doesn't need anymore to send
messages to the server, it calls a function which disconnect the
socket - so, when the DLL is unloaded, all the sockets are
normally already disconnected and are freed without errors.

But sometimes for external reasons this function is not called,
and the DLL is unloaded while some sockets aren't disconnected.
The destructor of my list looks like that :

destructor TCachedSocketSystem.Destroy;
begin
  DisconnectAll; //Disconnect all connected sockets
  FGCThread.SetThreadTerminated;
  FGCThread.WaitFor;
  FGCThread.Free;
  FLockCS.Free;
  inherited;
end;

...where DisconnectAll is a function which disconnects all the
sockets which remains connected :

procedure TCachedSocketSystem.DisconnectAll;
var
  I: Integer;
begin
  FLockCS.Acquire;
  try
    try
      for I := 0 to Pred(Count) do
        Items[I].Disconnect;  <- Exception raised here
    except
    end;
  finally
    FLockCS.Release;
  end;
end;

On this function, when a connected socket is disconnected the
following exception is raised :

Le projet tbsadminapp.exe a provoqué une classe d'exception
EIdSocketError avec le message 'Socket Error # 10091
'.  Processus stoppé. Utilisez Pas-à-pas ou Exécuter pour
continuer.

According by MSDN,

"WSASYSNOTREADY - 10091 : Network subsystem is unavailable.
    This error is returned by WSAStartup if the Windows Sockets
implementation cannot function at this time because the
underlying system it uses to provide network services is
currently unavailable. [...]"

I think this error is raised since I try to make a call to the
network *while the DLL is being unloaded* - I suppose that what
they call "the underlying system" has been unloaded before and is as a result unavailable when TIdTCPClient.Disconnect is called.
Nevertheless, my Winsock knowledge is very limited and it could
be inaccurate or completely wrong. But anyway, an exception is
raised here.

Currently, using a try/except construction allows me to "ignore"
the exception, but I don't know if it's best solution, or if I
can use others strategies in order to avoid this problem. So if
you already encountered this issue, I will happy to know what
you've done to solve it :-)

Best regards,

A.R.

Replies:

www.cryer.info
Managed Newsgroup Archive