Article View: borland.public.cppbuilder.internet.socket
Article #4988Possible memory leak with TIdTCPClient?
From: Drew
Date: Mon, 08 Nov 2004 22:36
Date: Mon, 08 Nov 2004 22:36
249 lines
7392 bytes
7392 bytes
My application has 10 threads each of which use a TIDTCPClient to connect to
a remote device. The threads typically run every 10 minutes to collect data
from a total of 126 sites.
Over time, the mmory used by my application increases, increasing from about
9Mb at start-up to more than 100 Mb after a day or so. I am fairly certain
that it is the Indy component that is causing the problem, because, by
changing to the ICS TWSocket component, the memory leak went away. However,
the ICS component doesn't seem as reliable as the Indy component, so I would
prefer to stay with this one.
Here is the code for the relevant class that is using the Indy component.
If anyone has any ideas or suggetions, then they would be most gratefully
received.
#include <vcl.h>
#pragma hdrstop
#include <dos.h>
#include <time.h>
#include "TCPCommPort.h"
#include "../Common/header.h"
//--------------------------------------------------------------------------
-----
TTCPCommPort::TTCPCommPort(int Aoffset, TScanThread* Aowner)
{
offset = Aoffset;
owner = Aowner;
m_clientSocket = new TIdTCPClient(NULL);
}
//--------------------------------------------------------------------------
-----
TTCPCommPort::~TTCPCommPort()
{
delete m_clientSocket;
}
//--------------------------------------------------------------------------
-----
bool TTCPCommPort::Open(void)
{
bool IPConnected = false;
// m_clientSocket->BoundPort = 54321 + offset;
m_clientSocket->Host = ipAddress;
m_clientSocket->Port = ipPort;
m_clientSocket->ReadTimeout = pollTimeout * 1000;
try
{
sprintf(owner->msg, "Just about to connect to TCP port in thread
%d", offset);
owner->Log(3);
m_clientSocket->Connect(ipTimeout * 1000);
sprintf(owner->msg, "Connected to TCP port in thread %d", offset);
owner->Log(3);
IPConnected = true;
}
catch (const Exception &connectErr )
{
sprintf(owner->msg, "Exception occurred in connection to TCP port in
thread %d: message",
offset, connectErr.Message);
owner->Log(3);
}
if (IPConnected)
{
CommOpen = true;
}
else
{
CommOpen = false;
}
return IPConnected;
}
//--------------------------------------------------------------------------
-----
void TTCPCommPort::Close(void)
{
try
{
if (GetConnected())
{
m_clientSocket->Disconnect();
}
CommOpen = false;
}
catch (Exception &e)
{
Application->ShowException(&e);
}
}
//--------------------------------------------------------------------------
-----
int TTCPCommPort::WriteBuffer(BYTE *buffer, unsigned int ByteCount)
{
if ((!GetConnected()) | (!CommOpen))
{
bool IPConnected = true; //Assume OK until we find otherwise
CommOpen = false;
try
{
m_clientSocket->Connect(ipTimeout * 1000);
}
catch (const EIdConnectTimeout &connectErr )
{
IPConnected = false;
}
catch (...)
{
IPConnected = false;
}
if (IPConnected)
{
CommOpen = true;
}
else
{
CommOpen = false;
}
}
if (GetConnected())
{
// Flush any existing received data from the stack and the internal
buffer
int dataLength = 0;
char* tempBuff;
try
{
// Use a short timeout because we just want to flush any
existing data
dataLength = m_clientSocket->ReadFromStack(true, 5, true);
}
catch (...)
{
// No data available to be read - return and rely on the retry
dataLength = 0;
}
if (dataLength > 0)
{
tempBuff = new char[dataLength];
m_clientSocket->ReadBuffer(tempBuff, dataLength);
delete[] tempBuff;
}
//Now send the new data
m_clientSocket->OpenWriteBuffer();
try
{
m_clientSocket->WriteBuffer(buffer, ByteCount, false); //Buffer
data
m_clientSocket->CloseWriteBuffer();
}
catch(...)
{
CommOpen = false;
return eTCPwriteFailed;
}
return eTCPwriteOK;
}
else
{
return eTCPwriteDisconnected;
}
}
//--------------------------------------------------------------------------
-----
int TTCPCommPort::ReadReply()
{
if (!CommOpen)
{
return 0;
}
try
{
bool finished = false;
int dataLength;
unsigned int Index=0;
char* tempBuff;
int shortTimeout = 100;
int fullTimeout=0;
while(!finished)
{
try
{
dataLength = m_clientSocket->ReadFromStack(true,
shortTimeout, true);
}
catch (...)
{
// No data available to be read. Add the shortTimeout to
the long
fullTimeout += shortTimeout;
dataLength = 0;
if (fullTimeout >= m_clientSocket->ReadTimeout)
{
sprintf(owner->msg, "Timeout occurred in reading reply
in thread %d", offset);
owner->Log(3);
finished = true;
}
}
if (dataLength > 0)
{
// Reset the full timeout counter, because we have received
some data
fullTimeout = 0;
tempBuff = new char[dataLength];
m_clientSocket->ReadBuffer(tempBuff, dataLength);
memcpy(&ReplyBuf[Index], tempBuff, dataLength);
Index += dataLength;
if (Index == ReplySize)
{
finished = true;
}
delete[] tempBuff;
}
}
return Index;
}
__finally
{
}
return 0;
}
//--------------------------------------------------------------------------
----
Many thanks
Andrew
Technical Details
Message-ID:
<418ff4b9$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.zanker.org!217.19.16.137.MISMATCH!news.cambrium.nl!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,357e5d574910e956,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Drew" <aemsley@brightwell.org.uk> Newsgroups: borland.public.cppbuilder.internet.socket Subject: Possible memory leak with TIdTCPClient? Date: Mon, 8 Nov 2004 22:36:28 -0000 Lines: 251 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 81.137.221.84 Message-ID: <418ff4b9$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1099953337 81.137.221.84 (8 Nov 2004 14:35:37 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.zanker.org!217.19.16.137.MISMATCH!news.cambrium.nl!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail