0

Getting runtime error in thread, error is "Unhandled exception at 0x0043e98e in clientCheck.exe: 0xC0000005: Access violation reading location 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");
                }
            }
        }

all values are updated in method disconnect.

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;
}

so please can anyone tell me what is the problem.?

4
  • where are you using locks to prevent races? Sep 13, 2011 at 14:10
  • ok, i edit my question i will put connect method also. Sep 13, 2011 at 14:15
  • Have you tried running it with a debugger, seeing which line the error arises? Then read the variable values in that context see which ones are wrong?
    – Shahbaz
    Sep 13, 2011 at 14:27
  • i am not getting value of m_tryToConnect using debugger. so there may be a problem. Sep 13, 2011 at 14:37

1 Answer 1

-1

It is not a good idea to use CreateThread() directly from C++ code. You should rather use _beginthreadex() to initialize C++ runtime environment for you thread (TLS variables and other stuff).

1
  • This is largely no longer true. Sep 13, 2011 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.