Program crashes need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mandogon
    New Member
    • Apr 2006
    • 31

    Program crashes need help

    Ok i have been looking at this block of code for about 2 weeks and cant figure out why my program keep on crashing within this block of code if you could help thanks

    Code:
    void __fastcall TPSM_OperatorClientForm::csktClientSocketRead(
          TObject *Sender, TCustomWinSocket *Socket)
    {
        try
    	{
        bool done = false;
        int bytes = 0;
        static int bytesLeft = 0;
        int msgBytes = 0;
        int offset = 0;
        struct PSM_Message *msg = 0;
    
        // Allocate memory to receive message
        static char *ptr;
        static char buf[MAX_SIZE];
        ptr = buf;
    
    	 // Read the message from the socket
        bytes = Socket->ReceiveBuf((void*)(ptr + bytesLeft), (MAX_SIZE - bytesLeft));
    
        // Set the bytes left value
        bytesLeft += bytes;
    
        // Are there messages to be dispatched
        int iTotalLen = 0;
        while(1)
        {
           void *cpyBuff = 0;
    
           // Get the 1st message
           msg = (struct PSM_Message *)&(ptr[offset]);
           msgBytes = msg->m_size;
    
    	    if(bytesLeft < msgBytes)//badri
    		return;
    
           // Allocate copy buffer for message
           cpyBuff = calloc(msg->m_size, sizeof(char));
    
    	   if( cpyBuff == NULL)
                return;
    
           // Copy the 1st message to the copy buffer
           ::memmove((void *)cpyBuff, (const void *)&(ptr[offset]), sizeof(char) * msgBytes);
           iTotalLen += sizeof(char) * msgBytes;
    
           // Move the offset
           offset += msg->m_size;
    
           if(msg->m_messageID == MSG_SM_OC_DATABASE_PATH_INFO)
           {
              WriteLog("OperatorCLientForm : Connected to server received - MSG_SM_OC_DATABASE_PATH_INFO");
              LogonRequest(msg);
              free(cpyBuff);
           }
           else
           {
    			   if (!::PostThreadMessage(pRouterThread->ThreadID, msg->m_messageID, (WPARAM)cpyBuff, 0))
    			   {
                      //Savita 11/dec/2001
                      AnsiString str = " PostMsgFailed its all down hill from here --The Error Code returned " + AnsiString(GetLastError());
                      WriteLog(L_ERROR,str.c_str());
                      //If it has not deleted also create this thread inorder to maintain the continuity
                      pRouterThread = NULL;
                      		pRouterThread = new PSM_RouterThread(false);
    
                      //Viswanath   12/dec/2001
                      if(pRouterThread != NULL)
                      {
            		    	WriteLog("OperatorCLientForm : Creating New Router Thread");
                      }
                      else
                      {
                        //Display a message stating that the resources is low. Shut down OC gracefully
                  		 //MessageBox(Handle,"System running low on resources.....Shutting down Operator Client","MMPOSEM Error",MB_OK |MB_SYSTEMMODAL );
                         MessageBox(Handle,GetString(IDS_ERR_SYSTEM_LOW_RESOURCES).c_str(),
                                        GetString(IDS_ERR_IPOS_ERROR).c_str(),
                                        MB_OK |MB_SYSTEMMODAL );
                         exit(-1);
                      }
    			   }
           }
    
           // adjust the bytes left value
           bytesLeft -= msg->m_size;
               if (bytesLeft < 0) bytesLeft = 0;
    
           // Check to see if we have dispatched ALL the messages in the buffer
           if(bytesLeft < 1) //badri
           {
              ::memmove((void *)ptr, (const void *)&(ptr[iTotalLen]), sizeof(char) * bytesLeft);
              return;
           }
    
           // Reset locals
           cpyBuff = 0;
           msg = 0;
    
        }
        // Free the Rx buffer
    //    free(ptr);
    
    #ifdef COMMOUT
         //Process the message
        try
        {
            if ( msg->m_messageID == MSG_SM_OC_DATABASE_PATH_INFO )
            {
                WriteLog("OperatorCLientForm : Connected to server received - MSG_SM_OC_DATABASE_PATH_INFO");
                LogonRequest(msg);
            }
            else
            {
                ::PostThreadMessage(pRouterThread->ThreadID, msg->m_messageID, (WPARAM)msg,0);
            }
        }
        catch(...)
        {
            //ShowMessage("Server not located");
            ShowMessage( GetString(ID_SVR_NOT_LOC) );
            PSM_TransFilterForm->TransGridHeaderDisplay();
        }
    #endif
    }
    	catch(...)
    	{
    		WriteLog(L_ERROR,"TPSM_OperatorClientForm :: csktClientSocketRead Failed ");
    	}
    }
    Last edited by AdrianH; May 5 '07, 01:26 AM. Reason: Please use [code][/code] tags for readability
  • mvbrevern
    New Member
    • Apr 2007
    • 5

    #2
    The piece of source is much too long. Please remove all parts that are
    definitly not responsible for the crash and post again. You probably even find
    your mistake yourself then.

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by mvbrevern
      The piece of source is much too long. Please remove all parts that are
      definitly not responsible for the crash and post again. You probably even find
      your mistake yourself then.
      Agreed.


      Adrian

      Comment

      • mandogon
        New Member
        • Apr 2006
        • 31

        #4
        I Have narrowed it down to these lines of code that are causing the my program to crash now i have commented out the if statement and noticed that it the program will run a lot longer but it will eventually crash if any one can help me figure out how i can fix this i will be very grateful it thanks
        Code:
        if(bytesLeft < 8000000)
        {
          ::memmove((void *)ptr, (const void *)&(ptr[iTotalLen]), sizeof(char) * bytesLeft);
          return;
        }
        Last edited by AdrianH; May 9 '07, 06:04 PM. Reason: Added code tags

        Comment

        Working...