2011-09-13 134 views
0

獲取在線程運行時錯誤,錯誤是「在0x0043e98e未處理異常clientCheck.exe:0000005:訪問衝突讀取位置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

你在哪裏使用鎖來阻止比賽? –

+0

好吧,我編輯我的問題,我會把連接方法也。 –

+0

你有沒有試過用調試器運行它,看看哪一行出現錯誤?然後在這種情況下閱讀變量值,看哪些是錯誤的? – Shahbaz

回答

-1

它不是直接從C++代碼中使用CreateThread()一個好主意。您應該使用_beginthreadex()爲您的線程初始化C++運行時環境(TLS變量和其他內容)。

+0

這在很大程度上不再是事實。 –