Article View: borland.public.cppbuilder.internet.socket
Article #5499Re: 'Out of memory' exception for a IdTCPServer
From: Drew
Date: Thu, 12 Aug 2004 22:00
Date: Thu, 12 Aug 2004 22:00
192 lines
6510 bytes
6510 bytes
Thanks for these comments.
The code for Log() is as follows:
//--------------------------------------------------------------------------
-
void __fastcall RTRSvrThread::Log(int logLevel, char* logMsg)
{
if ((ParentForm->logEnabled) & (logLevel <= ParentForm->logLevel))
{
struct time t;
gettime(&t);
lineToWrite.sprintf("%2d:%02d:%02d.%02d: ", t.ti_hour, t.ti_min,
t.ti_sec, t.ti_hund);
lineToWrite = lineToWrite + AnsiString(logMsg);
Synchronize(LogMsg);
}
}
//--------------------------------------------------------------------------
-
void __fastcall RTRSvrThread::LogMsg()
{
while (ParentForm->Memo1->Lines->Count > 1000)
{
ParentForm->Memo1->Lines->Delete(0);
}
ParentForm->Memo1->Lines->Add(lineToWrite);
}
I think that this is thread safe, is it not?
The code for the command handler that I suspect is as follows:
//--------------------------------------------------------------------------
-
void __fastcall RTRSvrThread::RtrServerUpdateHilltops(TIdCommand *ASender)
{
Log(2, "Updating hilltop info from RTR");
int msgType = ACK;
ASender->Thread->Connection->WriteLn(msgType);
Log(2, "ACKed updateHilltopInfo message to RTR");
SiteData_t* thisSiteInfo;
thisSiteInfo = new SiteData_t;
ASender->Thread->Connection->ReadBuffer(thisSiteInfo,
sizeof(SiteData_t));
{
sprintf(msg, "Read hilltop info %d from RTR", thisSiteInfo->siteID);
Log(2, msg);
}
// Use the point values returned by Rtr to update the database
time_t time_now;
time_now = time(NULL);
UpdatePointValues(thisSiteInfo, time_now);
// Now send a second ACK to Rtr to tell it that we have finished
updating the database
ASender->Thread->Connection->WriteLn(msgType);
Log(2, "ACKed hilltop data message to RTR");
// Find this hilltop on the PollActiveQ.
// Update the LastUpdate time and then move it
// from the PollActiveQ to one of the Idle Qs
SiteData_t* qHilltop;
for (int i=0; i < PollActiveQ->Count; i++)
{
qHilltop = (SiteData_t*)PollActiveQ->Items[i];
if (thisSiteInfo->siteID == qHilltop->siteID)
{
qHilltop->LastUpdate = time_now;
NormalIdleQ->Add(qHilltop);
PollActiveQ->Delete(i);
break;
}
}
// Finally delete the hilltop structure
try
{
delete thisSiteInfo;
}
catch(...)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default
language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
MessageBox( NULL, (LPTSTR)lpMsgBuf, "GetLastError",
MB_OK|MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
}
"Remy Lebeau (TeamB)" <gambit47.no.spam@no.spam.yahoo.com> wrote in message
news:411be31c$1@newsgroups.borland.com...
>
> "Drew" <aemsley@brightwell.org.uk> wrote in message
> news:411bdf12$1@newsgroups.borland.com...
>
> > RtrServer->Active = true;
> >
> > RtrServer->CommandHandlersEnabled = true;
>
> You should set up the command handlers before you activate the server, not
> afterwards.
>
> > RtrServer->CommandHandlers->Add();
> > RtrServer->CommandHandlers->Items[0]->Command = readRtrParams;
> > RtrServer->CommandHandlers->Items[0]->OnCommand =
> > RtrServerReadRtrParams;
>
> You should use the return value of Add() instead of indexing the Items[]
> collection over and over:
>
> TIdCommandHandler *Handler = RtrServer->CommandHandlers->Add();
> Handler->Command = readRtrParams;
> Handler->->OnCommand = RtrServerReadRtrParams;
>
> > void __fastcall RTRSvrThread::RtrServerDisconnect(TIdPeerThread
*AThread)
> > {
> > Log(0, "Rtr Server disconnected");
> > }
>
> What exactly does Log() do? Is its code thread-safe?
>
> > The error log shows the following:
> >
> > 19:59:43.56: Rtr Server exception: Out of memory
> > 19:59:43.56: Rtr server disconnected
>
> That suggests that one of your command handlers is probably where the
> exception is being thrown. Can you please show the actual handler code?
>
> > char* sendBuffer;
> >
> > sendBuffer = new
char[sizeof(SiteData_t)];
> >
> > memcpy(sendBuffer, site,
> sizeof(SiteData_t));
> >
> > Log(2, "Sending info buffer to DBM");
> > DbmClient->WriteBuffer(sendBuffer,
> > sizeof(SiteData_t));
> >
> > delete[] sendBuffer;
>
> Why allocate a new buffer? Just send the original data directly:
>
> DbmClient->WriteBuffer(site, sizeof(SiteData_t));
>
>
> > On the client side, the error log shows:
> >
> > 19:59:43.56: Sending updateHilltopInfo message to DBM
> > 19:59:43.56: Read of ACK from DBM failed
> > 19:59:43.56: Exception gnerated = Connection Closed Gracefully.
>
> That is to be expected since the server disconnected the client when the
> exception occured.
>
>
> Gambit
>
>
Technical Details
Message-ID:
<411be834@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<411b903e@newsgroups.borland.com> <411ba247$1@newsgroups.borland.com> <411bdf12$1@newsgroups.borland.com> <411be31c$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,56db4bd962ab0eb3 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Drew" <aemsley@brightwell.org.uk> Newsgroups: borland.public.cppbuilder.internet.socket References: <411b903e@newsgroups.borland.com> <411ba247$1@newsgroups.borland.com> <411bdf12$1@newsgroups.borland.com> <411be31c$1@newsgroups.borland.com> Subject: Re: 'Out of memory' exception for a IdTCPServer Date: Thu, 12 Aug 2004 23:00:40 +0100 Lines: 194 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: <411be834@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1092347956 81.137.221.84 (12 Aug 2004 14:59:16 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail