| Subject: | Number of OnConnect and OnDisconnect are not equal |
| Posted by: | Nguyen Diep |
| Date: | Wed, 31 Mar 2010 |
Hi everyone, I have this problem and I think many of people have as me, as reported in this forum.
If my http server program (IdHTTPServer on D2k9) assigns value to AContext.Data, the program may terminate after a few hours or a few days. My code is:
procedure TForm1.IdHTTPServer1OnConnect(AContext: TIdContext);
begin
AContext.Data := someobject;
...
end;
procedure TForm1.IdHTTPServer1OnDisconnect(AContext: TIdContext);
begin
...
AContext.Data := nil;
end;
If AContext.Data is not nil if client disconnects, an exception will be raised and program terminate, so in OnDisconnect event I must set AContext.Data := nil. But my program often terminates or the IdHTTPServer does not response (new connection will be reset), I speed a lot of time to find the reason but nothing found. I see that OnConnect event and OnDisconnect event are not equal. My trial code is :
var connectCount: integer = 0; disconnectCount: integer = 0;
procedure TForm1.IdHTTPServer1OnConnect(AContext: TIdContext);
begin
inc(connectCount);
end;
procedure TForm1.IdHTTPServer1OnDisconnect(AContext: TIdContext);
begin
inc(disconnectCount);
end;
procedure TForm1.Button1OnClick(Sender: TObject);
var currentCount: integer;
begin
try
currentCount := IdHTTPServer1.Contexts.LockList.Count;
finally
IdHTTPServer1.Contexts.UnlockList;
end;
Showmessage(Format('%d : %d : %d', [connectCount, disconnectCount, currentCount]));
end;
After running in 6 hours and accept 100K connections (approx 5 connections per second), I get the counter "105365 : 105334 : 16"
That means 15 OnDisconnect events were not triggered. And that may be the reason for my app terminating if it has AContext.Data assigned, because AContext.Data is not nil.
Does anyone encouter such a problem like that, if yes please tell me how to solve ?