Overview
Features
Download
Documentation
Community
Add-Ons & Services

Exception when a thread starts

Please post support and help requests here.

Exception when a thread starts

Postby Fu » 16 May 2007, 14:40

Hi,
I'm working on a TCP client program. (I've also done a little TCP server which seems to work fine to test it)
I've a "connect" function which establish a new connection like that :

^bool MyTCP : : connect(const std : : string host, const unsigned short hostPort)
{
try
{
if (mainGui) mainGui->setWxDiscutionText(wxString("Connection..."));
SocketAddress sa(host, hostPort);
MyTCPClient tcpClient(sa);
// run the tcpClient in a thread
Thread myThread;
myThread.start(tcpClient); // ---> cause an exception
return true;
}
catch (Exception& exc)
{
std : : cerr << exc.displayText() << std : : endl;
return false;
}
}^


Here is the MyTCPClient class :

^class MyTCPClient : public Runnable
{
public:
MyTCPClient(SocketAddress sa);
~MyTCPClient(void);
void run();
private:
StreamSocket sock;
};^

Today, the run() function doesn't do anything ( void MyTCPClient::run() { } ). It will display the text received from server, etc.

When executing the program, as soon as the "connect" function is called, i get an access violation exception when reading at 0x00000005.
If I don't call "myThread.start(tcpClient);" in the connect funtion, it works fine.
So, what am I doing wrong with thread ?
Fu
 
Posts: 4
Joined: 08 May 2007, 18:52

Re: Exception when a thread starts

Postby alex » 16 May 2007, 16:05

> Today, the run() function doesn't do anything ( void MyTCPClient::run() { } ). It will display the text received from server, etc.

To make it do anything useful, you must change the code so that your client and thread objects outlive the call to the connect function. The way things are now, they are both automatically destroyed when the connect() call is done.

Also, having const with parameters passed by values is legal but pointless. What you probably want is this:

Code: Select all

connect(const std::string& host, unsigned short hostPort)


Alex
alex
 
Posts: 1044
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Re: Exception when a thread starts

Postby Fu » 16 May 2007, 18:36

> To make it do anything useful, you must change the code so that your client and thread objects outlive the call to the connect function. The way things are now, they are both automatically destroyed when the connect() call is done.

Thank you ! It works fine now (:biggrin:)

> Also, having const with parameters passed by values is legal but pointless.

I've a lot of things to learn in C++ ! Thanks for the advice.
Fu
 
Posts: 4
Joined: 08 May 2007, 18:52


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest