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 ");
}
}
Comment