Click here to Skip to main content
16,005,080 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How do I implement a custom tag? Pin
dude_445316924-Mar-06 9:54
dude_445316924-Mar-06 9:54 
QuestionHow to Declare Pointer to Structure Global? Pin
fambi_mail22-Mar-06 8:22
fambi_mail22-Mar-06 8:22 
QuestionPOSTing binary data with HTTPS through wininet... Pin
dandy7222-Mar-06 8:22
dandy7222-Mar-06 8:22 
AnswerRe: POSTing binary data with HTTPS through wininet... Pin
MF22-Mar-06 16:12
MF22-Mar-06 16:12 
GeneralRe: POSTing binary data with HTTPS through wininet... Pin
dandy7223-Mar-06 2:43
dandy7223-Mar-06 2:43 
GeneralRe: POSTing binary data with HTTPS through wininet... Pin
dandy7223-Mar-06 4:16
dandy7223-Mar-06 4:16 
QuestionControling MS Communications Control? Pin
pavanbabut22-Mar-06 6:12
pavanbabut22-Mar-06 6:12 
QuestionOverlapped RS232 crash problem Pin
Battikaa22-Mar-06 5:49
Battikaa22-Mar-06 5:49 
Hi everyone,

I have a quite misterous problem. I am developing a windows application which is using the serial port for data transfere. I have developed a serial class based on the MSDN article Serial Communications in Win32. My application uses asnychron communication with overlapped structure.

The communication at the first sight is OK. But if there are more copies of my applications are running on the same PC using DIFFERNET serial ports, after few day of running and receiving datas suddenly appears the following messagebox by each one of the application at the same time:

Runtime Error! Program: myapp.exe This application has requested the Runtime to terminate it in an unusual way.

Each one of the applications stops receiving datas through the serial port, but their GUI is still active. As soon as I press the OK button on the dialog, each application closes.

I have succeed to figure out, that there is some kind of memory leak. The memory grows slowly and when it comes to certain point the Runtime error messagebox appears. Since the messagebox is on the the command returns an error:
OVERLAPPED osReader = {0};
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);


I guess I have to have some problem in the receiving routine. I went through my code several times but I couldn't find the error. There are 3 threads running 1 for the GUI, 1 for the serial data receiving and 1 for the serial data processing. The data are transfered between threads with deques.

What is very strange this error only happens if more than 1 of my applications are running. By 1 application everything is OK and the memory also stays at the same level.

Here is the receiving routine:

int UModulSerial::ReceiveVoid(LPVOID Buffer, int &NumberOfBytesToRead)
{

	//overlapped read, it's possible, that we have to wait with waitforsingle object till the read operation finishes
	if (m_bOverlapped)
	{
		CMutex mutRead;
		CSingleLock lock(&mutRead,TRUE);		//this will block other processes to read from the com port till our operation finishes
		m_sError = "";
		m_iErrNumber = 0;
		DWORD NumberOfBytesReceived=0;

		OVERLAPPED osReader = {0};
		osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
		if (osReader.hEvent == NULL)
		{
			SetErrorMessage(ERROR_CREATING_EVENT);				
			return -1;
		}


		int Read_Error = ReadFile
		(
			m_hSerPort,					// handle of file to read
			Buffer,						// pointer to buffer that receives data
			NumberOfBytesToRead,		// number of bytes to read
			&NumberOfBytesReceived,	// pointer to number of bytes read
			&osReader					// pointer to OVERLAPPED structure for data
		);

		if (!Read_Error)
		{
			DWORD LastErrorCode = GetLastError();						
			if (LastErrorCode == ERROR_IO_PENDING)     //if the read is not yet finished, wait till it finishes, or timeouts
			{							
				DWORD dwRes = WaitForSingleObject(osReader.hEvent, m_Comtimeout.ReadTotalTimeoutConstant);
				switch(dwRes)
				{					
					case WAIT_OBJECT_0:	// Read completed, transfer the overlapped result to our buffer.						
						if (!GetOverlappedResult(m_hSerPort, &osReader, &NumberOfBytesReceived, FALSE))
						{
							//if there was an error during transfering the overlapped structure to our buffer							
							SetErrorMessage(GetLastError());				
							CloseHandle(osReader.hEvent);
							return -1;
						}
						else
						{	
							//when the transfer was succesful, but the number of read bytes are not equal to requested than error
							if(NumberOfBytesToRead != NumberOfBytesReceived)
							{
								NumberOfBytesToRead = NumberOfBytesReceived;
								SetErrorMessage(ERROR_TIMEOUT);								
								CloseHandle(osReader.hEvent);
								return -1;
							}								
						}												
						break;
					case WAIT_TIMEOUT:
						//if there was a timeout during the read operation, than cancel the reading and return with error
						SetErrorMessage(ERROR_TIMEOUT);
						CancelIo(m_hSerPort);
						//we call this to get how many bytes were read till the timeout
						GetOverlappedResult(m_hSerPort, &osReader, &NumberOfBytesReceived, FALSE);						
						//maybe it was timeout but we received all the bytes we requested, in such case everything is ok
						if (NumberOfBytesToRead != NumberOfBytesReceived)
						{
							CloseHandle(osReader.hEvent);
							NumberOfBytesToRead = NumberOfBytesReceived;
							return -1;
						}
						else
							break;
						break;                       
					default:
						//this indicates, that there is a problem with the overlapped eventhandler
						SetErrorMessage(-1);
						CloseHandle(osReader.hEvent);
						return -1;
						break;
					}
				}
				else
				{
					// Error in communications; report it.
					SetErrorMessage(GetLastError());
					CloseHandle(osReader.hEvent);
					return -1;
				}		

			}
			
		CloseHandle(osReader.hEvent);
		if(NumberOfBytesToRead != NumberOfBytesReceived)
		{
			NumberOfBytesToRead = NumberOfBytesReceived;
			SetErrorMessage(ERROR_TIMEOUT);				
			return -1;
		}
		
		return 0;		//we return here, so the lock object will be destroyed in correct moment, when lock destroyed, it calls unlock
	}

Any help or comments are highly appriciated.
Greetings

Attila
AnswerRe: Overlapped RS232 crash problem Pin
Battikaa25-Apr-06 5:56
Battikaa25-Apr-06 5:56 
Questionplease help me Pin
dSolariuM22-Mar-06 5:26
dSolariuM22-Mar-06 5:26 
AnswerRe: please help me Pin
Dominik Reichl22-Mar-06 6:58
Dominik Reichl22-Mar-06 6:58 
QuestionNotification when a shared file is closed by other program Pin
bgsommerfeld22-Mar-06 4:58
bgsommerfeld22-Mar-06 4:58 
AnswerRe: Notification when a shared file is closed by other program Pin
Chris Meech22-Mar-06 7:05
Chris Meech22-Mar-06 7:05 
Questionvc.net(1.1) Pin
pankajgarg1222-Mar-06 4:06
pankajgarg1222-Mar-06 4:06 
AnswerRe: vc.net(1.1) Pin
toxcct22-Mar-06 4:09
toxcct22-Mar-06 4:09 
AnswerRe: vc.net(1.1) Pin
Dominik Reichl22-Mar-06 7:01
Dominik Reichl22-Mar-06 7:01 
Questionstructs in c++ Pin
pplshero5422-Mar-06 3:57
pplshero5422-Mar-06 3:57 
AnswerRe: structs in c++ Pin
toxcct22-Mar-06 4:07
toxcct22-Mar-06 4:07 
AnswerRe: structs in c++ Pin
Michael Dunn22-Mar-06 5:33
sitebuilderMichael Dunn22-Mar-06 5:33 
Questionwhat should be the salary of an exp vc++ sw developer Pin
vmmk22-Mar-06 3:22
vmmk22-Mar-06 3:22 
AnswerRe: what should be the salary of an exp vc++ sw developer Pin
toxcct22-Mar-06 4:11
toxcct22-Mar-06 4:11 
GeneralRe: what should be the salary of an exp vc++ sw developer Pin
Waldermort22-Mar-06 4:16
Waldermort22-Mar-06 4:16 
GeneralRe: what should be the salary of an exp vc++ sw developer Pin
toxcct22-Mar-06 4:19
toxcct22-Mar-06 4:19 
GeneralRe: what should be the salary of an exp vc++ sw developer Pin
Abebe22-Mar-06 4:21
Abebe22-Mar-06 4:21 
GeneralRe: what should be the salary of an exp vc++ sw developer Pin
V.22-Mar-06 4:40
professionalV.22-Mar-06 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.