BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Cleanup after disconnect

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
AntonE
Guest





PostPosted: Sat May 19, 2007 1:05 am    Post subject: Cleanup after disconnect Reply with quote



In my (Indy10) TCPserver, I have code in OnConnect:


AContext.Connection.IOHandler.WriteLn('TrueCalendar3 server ready...');
AContext.Connection.IOHandler.WriteLn('Version 1');
AContext.Data := TUserData.Create;
TUserData(AContext.Data).PeerIP
:=AContext.Connection.Socket.Binding.PeerIP;
TUserData(AContext.Data).HostName
:=GStack.HostByAddress(TUserData(AContext.Data).PeerIP);
TUserData(AContext.Data).Connected :=Now;
TUserData(AContext.Data).LastAction:=Now;


1)Now if he connection closes by client sending disconnect, will the
TUserData be freed automatically.
2)If so, then if the user disconnects by some unforseen method (network
failure), would it also be freed automatically?

I just do not want memory leaks to be present.

Thanks
AntonE
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sat May 19, 2007 2:09 am    Post subject: Re: Cleanup after disconnect Reply with quote



"AntonE" <antone (AT) true (DOT) co.za> wrote in message
news:464e078a (AT) newsgroups (DOT) borland.com...

Quote:
if he connection closes by client sending disconnect,
will the TUserData be freed automatically.

if the user disconnects by some unforseen method
(network failure), would it also be freed automatically?

TIdContext owns the Data member. TIdContext's destructor calls Free()
on it. There is no guarantee that the TIdContext will be freed right
away, though, so I would not suggest relying on that automatic
cleanup. You are allocating the TUserData in the OnConnect event, so
you should free it yourself in the OnDisconnect event and set the Data
property to nil, ie:.

AContext.Data.Free;
AContext.Data := nil;


Gambit
Back to top
AntonE
Guest





PostPosted: Sat May 19, 2007 3:03 am    Post subject: Re: Cleanup after disconnect Reply with quote



Thanks for the response.

Quote:
you should free it yourself in the OnDisconnect event and set the Data
property to nil, ie:.

AContext.Data.Free;
AContext.Data := nil;


I take it then the OnDisconnect gets called even in an unforseen
(ungracefull? Wink) disconnect?
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sat May 19, 2007 3:49 am    Post subject: Re: Cleanup after disconnect Reply with quote

"AntonE" <antone (AT) true (DOT) co.za> wrote in message
news:464e22c8 (AT) newsgroups (DOT) borland.com...

Quote:
I take it then the OnDisconnect gets called even in
an unforseen (ungracefull? Wink) disconnect?

Eventually, yes. It may take a long time for the OS to detect the
ungraceful loss and invalidate the socket, though. You should
implement your own timeout code so that you can close the connection
faster.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.