473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. void __fastcall TPSM_OperatorClientForm::csktClientSocketRead(
  2.       TObject *Sender, TCustomWinSocket *Socket)
  3. {
  4.     try
  5.     {
  6.     bool done = false;
  7.     int bytes = 0;
  8.     static int bytesLeft = 0;
  9.     int msgBytes = 0;
  10.     int offset = 0;
  11.     struct PSM_Message *msg = 0;
  12.  
  13.     // Allocate memory to receive message
  14.     static char *ptr;
  15.     static char buf[MAX_SIZE];
  16.     ptr = buf;
  17.  
  18.      // Read the message from the socket
  19.     bytes = Socket->ReceiveBuf((void*)(ptr + bytesLeft), (MAX_SIZE - bytesLeft));
  20.  
  21.     // Set the bytes left value
  22.     bytesLeft += bytes;
  23.  
  24.     // Are there messages to be dispatched
  25.     int iTotalLen = 0;
  26.     while(1)
  27.     {
  28.        void *cpyBuff = 0;
  29.  
  30.        // Get the 1st message
  31.        msg = (struct PSM_Message *)&(ptr[offset]);
  32.        msgBytes = msg->m_size;
  33.  
  34.         if(bytesLeft < msgBytes)//badri
  35.         return;
  36.  
  37.        // Allocate copy buffer for message
  38.        cpyBuff = calloc(msg->m_size, sizeof(char));
  39.  
  40.        if( cpyBuff == NULL)
  41.             return;
  42.  
  43.        // Copy the 1st message to the copy buffer
  44.        ::memmove((void *)cpyBuff, (const void *)&(ptr[offset]), sizeof(char) * msgBytes);
  45.        iTotalLen += sizeof(char) * msgBytes;
  46.  
  47.        // Move the offset
  48.        offset += msg->m_size;
  49.  
  50.        if(msg->m_messageID == MSG_SM_OC_DATABASE_PATH_INFO)
  51.        {
  52.           WriteLog("OperatorCLientForm : Connected to server received - MSG_SM_OC_DATABASE_PATH_INFO");
  53.           LogonRequest(msg);
  54.           free(cpyBuff);
  55.        }
  56.        else
  57.        {
  58.                if (!::PostThreadMessage(pRouterThread->ThreadID, msg->m_messageID, (WPARAM)cpyBuff, 0))
  59.                {
  60.                   //Savita 11/dec/2001
  61.                   AnsiString str = " PostMsgFailed its all down hill from here --The Error Code returned " + AnsiString(GetLastError());
  62.                   WriteLog(L_ERROR,str.c_str());
  63.                   //If it has not deleted also create this thread inorder to maintain the continuity
  64.                   pRouterThread = NULL;
  65.                           pRouterThread = new PSM_RouterThread(false);
  66.  
  67.                   //Viswanath   12/dec/2001
  68.                   if(pRouterThread != NULL)
  69.                   {
  70.                         WriteLog("OperatorCLientForm : Creating New Router Thread");
  71.                   }
  72.                   else
  73.                   {
  74.                     //Display a message stating that the resources is low. Shut down OC gracefully
  75.                        //MessageBox(Handle,"System running low on resources.....Shutting down Operator Client","MMPOSEM Error",MB_OK |MB_SYSTEMMODAL );
  76.                      MessageBox(Handle,GetString(IDS_ERR_SYSTEM_LOW_RESOURCES).c_str(),
  77.                                     GetString(IDS_ERR_IPOS_ERROR).c_str(),
  78.                                     MB_OK |MB_SYSTEMMODAL );
  79.                      exit(-1);
  80.                   }
  81.                }
  82.        }
  83.  
  84.        // adjust the bytes left value
  85.        bytesLeft -= msg->m_size;
  86.            if (bytesLeft < 0) bytesLeft = 0;
  87.  
  88.        // Check to see if we have dispatched ALL the messages in the buffer
  89.        if(bytesLeft < 1) //badri
  90.        {
  91.           ::memmove((void *)ptr, (const void *)&(ptr[iTotalLen]), sizeof(char) * bytesLeft);
  92.           return;
  93.        }
  94.  
  95.        // Reset locals
  96.        cpyBuff = 0;
  97.        msg = 0;
  98.  
  99.     }
  100.     // Free the Rx buffer
  101. //    free(ptr);
  102.  
  103. #ifdef COMMOUT
  104.      //Process the message
  105.     try
  106.     {
  107.         if ( msg->m_messageID == MSG_SM_OC_DATABASE_PATH_INFO )
  108.         {
  109.             WriteLog("OperatorCLientForm : Connected to server received - MSG_SM_OC_DATABASE_PATH_INFO");
  110.             LogonRequest(msg);
  111.         }
  112.         else
  113.         {
  114.             ::PostThreadMessage(pRouterThread->ThreadID, msg->m_messageID, (WPARAM)msg,0);
  115.         }
  116.     }
  117.     catch(...)
  118.     {
  119.         //ShowMessage("Server not located");
  120.         ShowMessage( GetString(ID_SVR_NOT_LOC) );
  121.         PSM_TransFilterForm->TransGridHeaderDisplay();
  122.     }
  123. #endif
  124. }
  125.     catch(...)
  126.     {
  127.         WriteLog(L_ERROR,"TPSM_OperatorClientForm :: csktClientSocketRead Failed ");
  128.     }
  129. }
May 4 '07 #1
3 1459
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.
May 5 '07 #2
AdrianH
1,251 Expert 1GB
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
May 5 '07 #3
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
Expand|Select|Wrap|Line Numbers
  1. if(bytesLeft < 8000000)
  2. {
  3.   ::memmove((void *)ptr, (const void *)&(ptr[iTotalLen]), sizeof(char) * bytesLeft);
  4.   return;
  5. }
May 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: J. Campbell | last post by:
what happens to allocated memory when a program terminates before the memory is released. For example: int main(){ int* array; int a_size = 1000; array = new int; for(int i = 0; i < a_size;...
10
by: Steven | last post by:
The following program runs alright until the return statement, where it crashes, and consequently, Dr. Watson (drwtsn32.exe) also crashes. Deleting the call to fgets() and everything runs fine. I...
8
by: Paul Bromley | last post by:
I am about to release the second version of a program that I started writing 12 months ago. The first version worked fine on 98 upwards. This version seems to crash on Windows 98. I realise that...
6
by: popone | last post by:
Hi All, I've developed a program that seems to crash randomly, from what I can tell it's after some user action. If it just sat there, it wouldn't crash. It's not an exception either, it's the...
7
by: Daniel | last post by:
I am baffled. According to the C++ faq lite it is ok to use a reference as an lvalue, and the subscript operator returns a reference. However, when I run this program, it crashes! I will go set up...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
3
by: linksterman | last post by:
I am coding for the PSP and I have an RSS reader that will show images and stuff. the proplem is that the reader will crash after displaying i think 10 pages with images on them. here is the part of...
10
by: Deviate | last post by:
Hi guys, my first post here, im desperately in need of help! :) When i am running Visual C++ 6.0 and have my .cpp file opened, i compile, build and run the program, it all works fine. But if i...
13
by: Thomas Neubauer | last post by:
Hello, i am learning c# and have created now a simple project that just creates 6 random numbers. My form includes a button and 6 labels for the random numbers. The program seems to work...
2
by: jaddle | last post by:
I program I'm writing crashes when it exits with a segfault. I'm runnning under linux. Using gdb, i see that the crash happens after the last line of main(), which is just a return(EXIT_SUCCESS); If...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.