🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #2384

Using TClientSocket in multi-threaded app

#2384
From: Mark Atkin
Date: Thu, 20 Apr 2006 20:25
40 lines
3147 bytes
I have a multi-threaded application where each thread uses TClientSocket to 
communicate to a device on the network.  I start a new thread for each 
device that I need to talk to.  My main form starts each of these threads, 
and when a timer on the main form reaches 0, a flag is set in each thread to 
tell the thead that it is time to talk to it's device.  The next time the 
thread checks it's status, the thread calls new TClientSocket to allocate a 
new socket connection to use for the communication, retrieves the data, then 
deletes it's instance of TClientSocket.  Everything seems to work fine on my 
computer.  When I tried it on a client's computer, I began getting AVs when 
the thread was calling new TClientSocket.  I was creating my instances of 
TClientSocket as follows:

TClientSocket *sock = new TClientSocket(MainForm);

where MainForm is the address of my form with the timer and the form that 
creates the threads.

My theory as to why I would get AVs is that maybe all three threads are 
calling new TClientSocket(MainForm) almost simultaneously and basically, 
some method and/or property from my MainForm is being accessed by all three 
threads to add the TClientSocket component to the ComponentList array and 
increment the ComponentCount for the MainForm.  I changed the socket 
instantiation to new TClientSocket(NULL) and the problem has gone away. 
This seems to agree with my theory, but it's just a theory.  The biggest 
hole in my theory that I can see is that it doesn't really explain why it 
would work on my PC and not my clients.  We are both running XPSP2 on P4s. 
It still could be a timing issue, but I would like to prove this theory 
somehow.  Also, the only advantage that I can see to having MainForm being 
the owner of the TClientSocket is that if something unexpected occurs and 
the TClientSocket doesn't get deleted somehow, the MainForm will free up the 
memory when the form gets destroyed.  This advantage is greatly decreased by 
the fact that the form is never destroyed until the program exits, in which 
case Windows is going to free up the memory anyway.

Any thoughts, opinions, or additional theories (or even proofs with examples 
:P) on this would be greatly appreciated.

Regards,

Mark Atkin
Technical Details
Message-ID: <4447ee51$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,f074aec44b23f651,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Mark Atkin" <matkin@windrock.com>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Using TClientSocket in multi-threaded app
Date: Thu, 20 Apr 2006 16:25:53 -0400
Lines: 42
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869
NNTP-Posting-Host: 69.2.92.6
Message-ID: <4447ee51$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1145564753 69.2.92.6 (20 Apr 2006 13:25:53 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail