在线程中获取运行时错误,错误是“clientCheck.exe中0x0043e98e处的未处理异常:0xC0000005:访问冲突读取位置0x025042c4”。
//create thread in cpp file
CreateThread(NULL,0,stayConnectedAtClient,this,0,NULL);
//thread definition in header file
static unsigned long __stdcall stayConnectedAtClient(void *i_socketTransPortClient){
((SocketTransportClient*)i_socketTransPortClient)->stayConnectedThread();
return 0;
}
//thread function defination in cpp file
void RMLThinTransport::SocketTransportClient::stayConnectedThread()
{
Sleep(20000);
OutputDebugStringW(L"this is stayconnected thread");
while(m_tryToConnect) // get error here, not getting value of m_tryToConnect it is Boolean and declared in class.
{
if(!m_isConnected) // m_isConnected value is also not updated even if it is changed by other function
{
break;
}
/* sleep for period of time given in configuration file */
Sleep(m_stayConnected);
if(!m_allreadyConnected)
{
bool isConnect=Connect();
if(isConnect)
{
m_allreadyConnected=true;
}
OutputDebugStringW(L"isConnect false");
}
}
}
所有值都在方法断开连接中更新。
bool RMLThinTransport::SocketTransportClient::disconnect()
{
m_isConnected=false;
m_tryToConnect=false;
notifyUserConnected(m_isConnected);
if(m_socketClient!=INVALID_SOCKET)
{
shutdown(m_socketClient,SD_BOTH);
closesocket(m_socketClient);
m_socketClient=INVALID_SOCKET;
}
Sleep(3000);
return false;
}
bool RMLThinTransport::SocketTransportClient::Connect()
{
try
{
m_socketClient = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(m_socketClient==INVALID_SOCKET){
int lastError;
lastError=WSAGetLastError();
SocketExceptions exceptionInOpenSocket;
exceptionInOpenSocket.detectErrorOpenSocket(&lastError);
throw exceptionInOpenSocket;
}
}
catch(SocketExceptions& i_exceptionInOpenSocket)
{
throw i_exceptionInOpenSocket;
return false;
}
memset(&m_SocketAddressIn,0,sizeof(m_SocketAddressIn));
m_SocketAddressIn.sin_family=AF_INET;
m_SocketAddressIn.sin_addr.s_addr=inet_addr(m_ccIPAddress);
m_SocketAddressIn.sin_port=htons(m_portNumber);
try
{
if(SOCKET_ERROR==connect(m_socketClient,(SOCKADDR *)&m_SocketAddressIn,sizeof(m_SocketAddressIn)))
{
m_allreadyConnected=false;
int lastErrorCode=WSAGetLastError();
SocketExceptions exceptionInConnection;
exceptionInConnection.detectErrorConnect(&lastErrorCode);
throw exceptionInConnection;
}
else
{
setConnected(true);
m_allreadyConnected=true;
if(m_evesdropString!=""){
char* charbuf=new char[m_evesdropString.size()+1];
std::copy(m_evesdropString.begin(),m_evesdropString.end(),charbuf);
charbuf[m_evesdropString.size()]='\0';
int iResult=send(m_socketClient,charbuf,strlen(charbuf),0);
memset(charbuf,0x00,sizeof(charbuf));
}
CreateThread(NULL,0,receiveClientData,this,0,NULL);
return true;
}
}
catch(SocketExceptions& i_exceptionInConnection)
{
shutdown(m_socketClient,SD_BOTH);
closesocket(m_socketClient);
m_socketClient=INVALID_SOCKET;
this->exceptionOccurred(EST_EXCEPTIONINCONNECT);
return false;
}
return true;
}
所以请任何人告诉我这是什么问题。?
答案 0 :(得分:-1)
直接从C ++代码使用CreateThread()
不是一个好主意。您应该使用_beginthreadex()
为线程初始化C ++运行时环境(TLS变量和其他东西)。