windows - runtime fejl i tråd i c ++

Indlæg af Hanne Mølgaard Plasc

Problem



Får runtime fejl i tråd, fejl er 'Uhåndteret undtagelse ved 0x0043e98e i clientCheck.exe: 0xC0000005: Adgang til overtrædelseslæsningssted 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");
                }
            }
        }


alle værdier opdateres i metodeafbrydelse.


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()]='';
                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;
}


så vær venlig at nogen kan fortælle mig, hvad der er problemet.?

Bedste reference


Det er ikke en god ide at bruge CreateThread() direkte fra C ++-koden. Du bør hellere bruge \_beginthreadex() til at initialisere C ++ runtime miljø for dig tråd (TLS variabler og andre ting).