 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
enzo Guest
|
Posted: Thu Feb 22, 2007 4:06 pm Post subject: system error code 6 when closing TClientSocket |
|
|
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();
}
} |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Feb 22, 2007 4:47 pm Post subject: Re: system error code 6 when closing TClientSocket |
|
|
"enzo" <enzo.arlati (AT) aesys (DOT) it> wrote in message
news:mn.b29a7d72294c86fa.47611 (AT) aesys (DOT) it...
| Quote: | 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 |
|
| Back to top |
|
 |
enzo Guest
|
Posted: Fri Feb 23, 2007 10:50 pm Post subject: Re: system error code 6 when closing TClientSocket |
|
|
Sembra che Remy Lebeau (TeamB) abbia detto :
| Quote: | . 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 |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sat Feb 24, 2007 12:34 am Post subject: Re: system error code 6 when closing TClientSocket |
|
|
"enzo" <enzo.arlati (AT) aesys (DOT) it> wrote in message
news:mn.bc2e7d7278fa63c8.47611 (AT) aesys (DOT) it...
| Quote: | 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 |
|
| Back to top |
|
 |
Lesley Anne Guest
|
Posted: Sat Feb 24, 2007 1:26 am Post subject: Re: system error code 6 when closing TClientSocket |
|
|
"enzo" <enzo.arlati (AT) aesys (DOT) it> wrote in message
news:mn.bc2e7d7278fa63c8.47611 (AT) aesys (DOT) it...
| Quote: |
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? |
|
| Back to top |
|
 |
enzo Guest
|
Posted: Sun Feb 25, 2007 3:09 am Post subject: Re: system error code 6 when closing TClientSocket |
|
|
"Lesley Anne" <mspfila (dash) brl (at) yahoo (dot) com> wrote:
| Quote: | "enzo" <enzo.arlati (AT) aesys (DOT) it> wrote in message
news:mn.bc2e7d7278fa63c8.47611 (AT) aesys (DOT) 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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|