🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages Started by enzo Thu, 22 Feb 2007 10:06
system error code 6 when closing TClientSocket
#1331
Author: enzo
Date: Thu, 22 Feb 2007 10:06
208 lines
7157 bytes
Using c++builder 6 I build a very simple client, which call a thread to 
connect to a server.
The first connection works well, while when I try to close the thread 
with Terminate and waitFor
I got a system error code 6.
Here a snippet of the log with the example and after tha sample code I 
used.
For the connection I use the standard TClientSocket component, used in 
blocking mode
There is something wrong when I close the thread but I can't gues what.
Please, can someone give me some hint on where I wrong .
Regards, Enzo


[22/02/2007 10.47.12] [ThrID=00E8] [Execute] Nuova connessione: 
192.168.200.218:2023 --> 192.168.200.47:19001
[22/02/2007 10.47.19] [ThrID=00E8] [Execute] END  [ connessione: 
192.168.200.218:2023 --> 192.168.200.47:19001 ]
[22/02/2007 10.47.19] [ThrID=00E8] [ThClientSocket::~ThClientSocket] 
END
[22/02/2007 10.47.23] [BtnConnect-KillConn]ERRORE System Error.  Code: 
6.

Handle non valido


***************************


ThClientSocket * thClntSckt;


void __fastcall TForm1::BtnConnectClick(TObject *Sender)
{
   try
   {
      // -------------------------------------------------------
      try
      {
          if( thClntSckt )
          {
             thClntSckt->Terminate();
             thClntSckt->WaitFor();
             thClntSckt = 0;
             return;
          }
      }
      catch( const Exception & ex )
      {
         String serr = Format( "[BtnConnect-KillConn]ERRORE %s ", 
ARRAYOFCONST(( ex.Message )) );
         Util::mylog( serr );
         return;
      }
      // -------------------------------------------------------
      try
      {
          thClntSckt = new ThClientSocket( true );
          thClntSckt->FreeOnTerminate = true;
          thClntSckt->IPAddress = EditHost->Text;
          thClntSckt->Port      = StrToIntDef( EditPort->Text, 0 );
          thClntSckt->Resume();
      }
      catch( const Exception & ex )
      {
         String serr = Format( "[BtnConnect-NewConn]ERRORE %s ", 
ARRAYOFCONST(( ex.Message )) );
         Util::mylog( serr );
      }
      // -------------------------------------------------------
   }
   catch( const Exception & ex )
   {
      String serr = Format( "[BtnConnect]ERRORE %s ", ARRAYOFCONST(( 
ex.Message )) );
      Util::mylog( serr );
   }
}



/******************************************************
  TClientSocket * mySocket
 ******************************************************/
__fastcall ThClientSocket::ThClientSocket(bool CreateSuspended)
        : TThread(CreateSuspended)
{
    mySocket = new TClientSocket( NULL );
}



/******************************************************
 ******************************************************/
__fastcall ThClientSocket::~ThClientSocket()
{
   String stmp;
   try
   {
       if( mySocket )
       {
          if( mySocket->Active )
          {
              stmp = Format( "[Execute] Chiudi connessione: %s:%d --> 
%s:%d ",
                       ARRAYOFCONST(( mySocket->Socket->LocalAddress,
                                      mySocket->Socket->LocalPort,
                                      mySocket->Socket->RemoteAddress,
                                      mySocket->Socket->RemotePort )));
              mylog( stmp );
          }
          mySocket->Close();
          while( mySocket->Active )
          {
             mylog( "[ThClientSocket::~ThClientSocket] END close 
Socket" );
             Sleep( 500 );
          }
          while( mySocket->Socket->Connected )
          {
             mylog( "[ThClientSocket::~ThClientSocket] END close 
winSocket" );
             Sleep( 500 );
          }
          delete mySocket;
          mySocket = 0;
       }
   }
   catch( const Exception & ex )
   {
      String serr = Format( "[ThClientSocket::~ThClientSocket]ERRORE %s 
", ARRAYOFCONST(( ex.Message )) );
      Util::mylog( serr );
   }
   mylog( "[ThClientSocket::~ThClientSocket] END " );
}



void __fastcall ThClientSocket::Execute()
{

   int fSocketTimeout = 30;
   TWinSocketStream * pStream = 0;
   char buffer[BUFFERSIZE];
   int nloop = 0;
   String sMsg, sMsgInp, stmp;
   int dt1, dt2;

   try
   {
       // -------------------------------------------------------
       if( ! mySocket->Active )
       {
          mySocket->Address = fIPAddress;
          mySocket->Port    = fPort;
          mySocket->ClientType = ctBlocking;
          mySocket->Open();

          stmp = Format( "[Execute] Nuova connessione: %s:%d --> %s:%d 
",
                   ARRAYOFCONST(( mySocket->Socket->LocalAddress,
                                  mySocket->Socket->LocalPort,
                                  mySocket->Socket->RemoteAddress,
                                  mySocket->Socket->RemotePort )));
          mylog( stmp );
       }
       // -------------------------------------------------------
       while( !Terminated && mySocket->Active )
       {
          if( pStream == 0 )
          {
             pStream = new TWinSocketStream(mySocket->Socket, 
fSocketTimeout );
          }

          *********          Here the working code     ********
          *********  but is commented for this example ********


       }
       // -------------------------------------------------------
       stmp = Format( "[Execute] END  [ connessione: %s:%d --> %s:%d 
]",
                 ARRAYOFCONST(( mySocket->Socket->LocalAddress,
                                mySocket->Socket->LocalPort,
                                mySocket->Socket->RemoteAddress,
                                mySocket->Socket->RemotePort )));
       mylog( stmp );
       // -------------------------------------------------------
       if( pStream )
       {
           pStream->WaitForData(fSocketTimeout);
           delete pStream;
           pStream = 0;
       }
       mySocket->Close();
       // -------------------------------------------------------
   }
   catch( const Exception & ex )
   {
      String serr = Format( "[ThClientSocket::Execute]ERRORE %s ", 
ARRAYOFCONST(( ex.Message )) );
      mylog( serr );
      mySocket->Close();
   }

}
Re: system error code 6 when closing TClientSocket
#1232
Author: Remy Lebeau (TeamB)
Date: Thu, 22 Feb 2007 10:47
34 lines
1947 bytes
"enzo" <enzo.arlati@aesys.it> wrote in message
news:mn.b29a7d72294c86fa.47611@aesys.it...

> when I try to close the thread with Terminate and waitFor
> I got a system error code 6.

6 = ERROR_INVALID_HANDLE

You are setting the thread's FreeOnTerminate property to true.  You
can't use WaitFor() and FreeOnTerminate together.  That is a bug that
was introduced in BCB 6.  If you want to use WaitFor(), then leave
FreeOnTerminate set to its default value of false and then delete the
thread yourself after WaitFor() exits, ie:

    if( thClntSckt )
    {
        thClntSckt->Terminate();
        thClntSckt->WaitFor();
        delete thClntSckt; // <-- add this
        thClntSckt = NULL;
        return;
    }

    {
        thClntSckt = new ThClientSocket(true);
        // thClntSckt->FreeOnTerminate = true; // <-- Get rid of this
        thClntSckt->IPAddress = EditHost->Text;
        thClntSckt->Port = EditPort->Text.ToIntDef(0);
        thClntSckt->Resume();
      }


Gambit
Re: system error code 6 when closing TClientSocket
#1249
Author: enzo
Date: Fri, 23 Feb 2007 16:50
21 lines
1738 bytes
Sembra che Remy Lebeau (TeamB) abbia detto :
> .  You
> can't use WaitFor() and FreeOnTerminate together.  That is a bug that
> was introduced in BCB 6.

thanks for the suggestion, it's working.

about BCB 6 I have another , unclear and complicated question.
I have wondering if the BCB6 ( respect the BCB5 ) has some problem 
which make cause some leak of memory.
I have experienced a lot of memory leak when the program is running, 
when I close the program all the memory is released, but I need a 
program which can run for a very long time.
I using a lot of socket, thread and most of my program are also server 
com.
Both trhreads and socket during th time are created and destroyed 
several times.
mybe in some of this topic there is something I should care to avoid 
loss of memory.

Regards, Enzo
Re: system error code 6 when closing TClientSocket
#1250
Author: Remy Lebeau (TeamB)
Date: Fri, 23 Feb 2007 18:34
12 lines
1318 bytes
"enzo" <enzo.arlati@aesys.it> wrote in message
news:mn.bc2e7d7278fa63c8.47611@aesys.it...

> I have wondering if the BCB6 ( respect the BCB5 ) has some
> problem which make cause some leak of memory.

More than likely, the leaks are in your own code.  You are probably
not freeing everything you allocate.


Gambit
Re: system error code 6 when closing TClientSocket
#1251
Author: Lesley Anne
Date: Fri, 23 Feb 2007 19:26
9 lines
1350 bytes
"enzo" <enzo.arlati@aesys.it> wrote in message 
news:mn.bc2e7d7278fa63c8.47611@aesys.it...
>
> I have experienced a lot of memory leak when the program is running, when 
> I close the program all the memory is released, but I need a program which 
> can run for a very long time.

Have you tried using CodeGuard to identify where the memory leak is coming 
from?
Re: system error code 6 when closing TClientSocket
#1259
Author: enzo
Date: Sat, 24 Feb 2007 21:09
14 lines
1464 bytes
"Lesley Anne" <mspfila (dash) brl (at) yahoo (dot) com> wrote:

> "enzo" <enzo.arlati@aesys.it> wrote in message
> news:mn.bc2e7d7278fa63c8.47611@aesys.it...
>>
>> I have experienced a lot of memory leak when the program is running, when
>> I close the program all the memory is released, but I need a program
>> which can run for a very long time.
> 
> Have you tried using CodeGuard to identify where the memory leak is coming
> from?

For CodeGuard is all ok, but WorkingSet (obtained using the
GetProcessMemoryInfo API ) still grow up
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads