473,403 Members | 2,323 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,403 software developers and data experts.

Threading and Sockets

Hi,

I have a program that reads information from an IP device (camera). It establishes the Telnet connection and then via a thread, initializes the camera (runs a bunch of commands to get the camera's basic configuration). I keep getting the same two errors: "User called breakpoint at 0xXXXXXX" or Access violation. It seems that it always quits whenever I do a CString concatenation on the receive side. I'm using a CString to act as a FIFO buffer and process whatever data is in the buffer. If I step through the program in debug, it often works fine, but with no breakpoints set, it crashes intermittently. I use CCriticalSection to try and protect the operations that modify the buffer and it reduces it, but doesn't eliminate it. Any ideas? Thanks.

Vinoj
May 22 '09 #1
6 2630
mac11
256 100+
I'll try to offer some general ideas, but mostly plead for more details...

I would bet that if you're getting intermittent crashes it's likely some buffer overrun, or unbound array access where you're clobbering memory. Though I have never seen this particular error and it could end up being something completely different.

What OS and compiler are you using?

Are you doing memory allocation and memory copy stuff alot? Maybe it would help if you can use c++ stl containers (string, streams, streambuf, and such) so you won't have to worry about memory management so much (I assume you are).

It's possible something in your thread handling is causing the issue, maybe you can try to post some code example?
May 23 '09 #2
RRick
463 Expert 256MB
The access violation means you are trying to access some restricted area. This is usually caused by bad pointers or bad data. Unfortunately, there are a lot of things that can cause this type of memory corruption.

A memory checker would be helpful in this situation, or logs of the pointer values/data before you try to access the string.

You mentioned you are using threads. You might have a race condition where one thread is adding data and another is extracting or accessing the data before the data is available. Its typical for a race condition to not show up in the debugger, because breakpoints, etc. tend to slow down the program and the race condition doesn't happen.
May 24 '09 #3
mac11, I'm using XP and VC++ 6. I'm using CStrings so I think that is a self-managed entity. I add received data to a CString buffer, and delete it when I've processed it. I wasn't doing any explicit memory management.

RRick, I run the heap check function to check the status of the heap and it always returns ok. Is there a good/free memory checker tool you'd recommend? I'll try and post the errors and some code.
May 26 '09 #4
Ok,

Here's the code causing problems. I've cut out some of the repetitive parts to make it more concise. The code calls ReceiveTCP to append rx'd data to a CString buffer, then calls OnTcpip() to process the data. The error occurs during a CString destructor - FreeData call.

Expand|Select|Wrap|Line Numbers
  1. int CIrCamera::SendTCP()
  2. {
  3.     _critSect.Lock();
  4.     int bytesSent = 0, nRet = 0;
  5.     char toSend = 0;
  6.     CString tempSendBuff = m_strSendBuff;
  7.  
  8.     //insert delay on sending, 5 ms per character, 50 ms per line
  9.     while(tempSendBuff.GetLength() > 0){
  10.         if(tempSendBuff[0] == '\n')// || tempSendBuff[0] == '\r')
  11.             Sleep(50);
  12.         else
  13.             Sleep(5);
  14.         toSend = tempSendBuff[0];
  15.         bytesSent += m_pSocket->Send(&toSend, 1, 0);
  16.         if(bytesSent < 0){
  17.             bytesSent = GetLastError();
  18.             break;            
  19.         }
  20.         tempSendBuff.Delete(0);
  21.     }
  22.     m_strSendBuff.Empty();
  23.     tempSendBuff.Empty();
  24.  
  25.     if(bytesSent == WSAECONNABORTED || bytesSent == WSAECONNRESET){
  26.         Disconnect();
  27.         Connect();
  28.     }
  29.     m_iPending++;
  30.  
  31.     //giving time for responses to 
  32.     Sleep(50);
  33.     _critSect.Unlock();
  34.  
  35.     return bytesSent;
  36. }
  37.  
  38. int CIrCamera::ReceiveTCP() 
  39. {
  40.     _critSect.Lock();
  41.     int nRet = 0;
  42.     memset(m_cBuff, 0, sizeof(m_cBuff));
  43.     nRet = m_pSocket->Receive(m_cBuff, BUFFER_SIZE, 0);
  44.     temp = m_strRecvBuff + m_cBuff;
  45.     m_strRecvBuff += m_cBuff;
  46.     _critSect.Unlock();
  47.     return nRet;
  48. }
  49.  
  50. void CIrCamera::OnTcpip()
  51. {
  52.  
  53.     int bFailedOpCtr = 0;
  54.     _critSect.Lock();
  55.  
  56.     while(m_strRecvBuff.GetLength() > 0 && !m_bIncompleteRx){
  57.         CheckForTelnetText();
  58.         switch(m_iCurrentState){
  59.             case 0:                
  60.                 //initialize telnet
  61.                 TelnetInit();
  62.                 m_iPending=0;
  63.                 break;
  64.             case 1:
  65.                 m_iPending--;
  66.                 //telnet command processing state
  67.                 switch(GetCurrentOperation()){
  68.                     case 1:
  69.                         m_bOpCompleted = ReadFOVLimits();
  70.                         break;
  71.                     case 2:
  72.                         m_bOpCompleted = ReadFocusLimits();
  73.                         break;
  74.                     case 3:
  75.                         m_bOpCompleted = ReadFOV();
  76.                         break;
  77.                     case 4:
  78.                         m_bOpCompleted = ReadFocus();
  79.                         break;
  80. //..cut out case statements that call parsing functions
  81.           case -1:
  82.                         m_bIncompleteRx = TRUE;
  83.                         break;
  84.                 }
  85.                 if(m_strRecvBuff.GetLength() <=0)
  86.                     m_pExtDoc->StoreIrCamera();
  87.  
  88.                 break;                
  89.         }
  90.         //if a failed op, reset buffer in case of garbage
  91.         if(!m_bOpCompleted){
  92.             bFailedOpCtr++;
  93.             if(bFailedOpCtr > 25)
  94.                 m_strRecvBuff.Empty();
  95.         }
  96.  
  97.     }
  98.     //when rx completes, check current op
  99.     if(m_bOpCompleted){
  100.         m_cCurrentOperation = 0;
  101.         m_bIncompleteRx = FALSE;
  102.     }
  103.     _critSect.Unlock();
  104.  
  105. }
  106.  
  107. char CIrCamera::GetCurrentOperation()
  108. {
  109.  
  110.     //store the global in case an incomplete command comes through
  111.     _critSect.Lock();
  112.     char currentOp = m_cCurrentOperation;
  113.     CString tempRecvBuff = m_strRecvBuff;
  114.  
  115.     //if there is intermittent telnet commands in the stream
  116.     StripSpecialChars();
  117.  
  118.     if(m_bOpCompleted){
  119.         CString temp;
  120.         temp = tempRecvBuff.Left(tempRecvBuff.Find('\n'));
  121.         //if no '\n' in buffer
  122.         if(temp.GetLength() == 0){
  123.             temp = tempRecvBuff;
  124.         }
  125.  
  126.         //Set Commands - do not require parsing
  127.         if(temp.Find("overlay on") > -1){
  128.             tempRecvBuff.Delete(0, temp.GetLength()+1);
  129.             currentOp = 0;
  130.         }
  131.         else if(temp.Find("overlay off") > -1){
  132.             tempRecvBuff.Delete(0, temp.GetLength()+1);
  133.             currentOp = 0;
  134.         }
  135.         //... cut out a bunch of find() commands...//
  136.         }
  137.         //Telnet Echo
  138.         else if(temp.Find(m_strSend) > -1){
  139.             tempRecvBuff.Delete(0, temp.GetLength()+1);
  140.             currentOp=10;
  141.         }
  142.         else{
  143.             currentOp = -1;
  144.         }
  145.  
  146.         //set the global
  147.         m_cCurrentOperation = currentOp;
  148.     }
  149.     m_strRecvBuff.Empty();
  150.     m_strRecvBuff = tempRecvBuff;
  151.     _critSect.Unlock();
  152.     return m_cCurrentOperation;
  153. }
May 27 '09 #5
The main error right now are Access Violations in the ReceiveTCP() function during the concatenation of the string (the +=). I think I may have a race condition as previously mentioned. How do I check for that? I thought CriticalSections prevented more than one thread from accessing a section...
May 28 '09 #6
gadams
1
I would try looking into a .net hotfix. I have found a similar issue with a telnet device and a race condition, that I believe we are having with our intermec scan guns. Please see hotfix: http://support.microsoft.com/kb/2759112 for asp.net. This is for a later issue of windows.... but, I believe from what you describe a very similar issue.
Jul 11 '13 #7

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

Similar topics

16
by: Rob Snyder | last post by:
Greetings - I've embarked on a small learning project, trying to get a better grasp of the asyncore and asynchat modules. The project involves building a simple instant messenger application for...
0
by: Darren Thomas | last post by:
Dear all, I have written a TCP server as a windows service that accepts connections using the System.Net.Sockets.Socket class. I use the various asynchronous methods in the socket class. I'm...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
2
by: AtherMurtuzapurwala | last post by:
Hi All, Client/Server Program using Sockets... Server sends request in form of CSV format in NetworkStream... So I want to read this using threading because it is in very large volume so...
0
by: richard | last post by:
OS: Winxp and Win2003 Visual Basic.NET 2003 MS-SQL Server 2000 hey all I am a newbie in vb.net but i have managed to build a simple chat server in vb.net using socket and a client connecting...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
7
by: Diego F. | last post by:
Hello. I have a doubt about multi-threading, if it is or not the best way to achieve the following: I have a server application that listens a port through a socket. A client will send messages...
5
by: Yehia A.Salam | last post by:
Hello, I am building a network application that make use of .Net Sockets, I created a class that works like a server and fires an event when anything arrives at the server, however I ran into...
1
by: RFleming | last post by:
I have a class that spawns a thread in a sub class to monitor data on a socket connection. Once data is found I read it as bytes and want to send it back to the parent class to a...
13
by: Alexander Gnauck | last post by:
Hello, while using async sockets I ran into a strange problem which occurs only on some machines. I wrote a small demo application which can be used to reproduce the problem. You can download it...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.