The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Special Forums > IP Networking
.
google unix.com



IP Networking Learn TCP/IP, Internet Protocol, Routing, Routers, Network protocols in this UNIX and Linux forum.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with memory leak kshk123 HP-UX 2 05-25-2009 07:01 AM
memory leak problem sonali High Level Programming 5 05-25-2009 06:55 AM
Memory leak in pthread mindTeaser UNIX for Advanced & Expert Users 4 05-18-2009 01:30 AM
Memory leak of fork() whererush High Level Programming 7 05-11-2006 11:51 AM
about virtual memory and memory leak shriashishpatil High Level Programming 4 02-20-2006 11:31 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-16-2005
lenna lenna is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 3
memory leak?

Hi All,

my client server application can work in two modes:
1) one direction - only client sends msgs to server
2) two directions - server gives 'answers' to client.

when program run in the first mode it looks OK, but when server answers to client than client's application exit its operation after a short while. I am trying to find the problem but have some taugh time.

this is the function that reads the data in the client side (in which I have problems):


Code:

int TCPClient::readSocketData(int s,
              char *decodeUnifiedMsgForServer,			
              int n,			
	 bool& isMsg
             )
{
	int bcount;							int br;							
	bcount = 0;
	br = 0;
	while (bcount < n)					
	{
		if ((br = recv(s,decodeUnifiedMsgForServer,n-bcount,0)) > 0)
		{
			isMsg = true;
			bcount += br;							decodeUnifiedMsgForServer+= br;              
		}
		else if (br < 0)             /* signal an error to the caller */
		{
			return(-1);
		}
		else							             {
			return bcount;
		}
		//Y 17_04_05 -
		Sleep(0);
		//Y.
                   }
  return(bcount);
}
after buffer (decodeUnifiedMsgForServer) is filled by the function above I decode the data into the relevant data classes as follows:


Code:

void BrainControlComData::decodeMsg(char* decodeUnifiedMsgForServer)
{



	if(strlen(decodeUnifiedMsgForServer) == 0)			
             {
		char* error = "probably an error (see Q1 below)";
	}


	char* msg = decodeUnifiedMsgForServer;
	int size = 4;

	int lenCursorData;
	int lenManipData;
	int lenVzData;

//places 1 - 12 in msg are reserved for total size/ isEventMsg/ MsgID

	int Web_lenOfClass;					
// decode len of msg
	memcpy(&Web_lenOfClass, msg, size);
	int lenOfClass = ntohl(Web_lenOfClass);

	int Web_MsgID;						
// decode msg ID
	memcpy(&Web_MsgID, msg + MSG_ID_LOCATION, size);
	int msgID = ntohl(Web_MsgID);



	//Y 6_07_05 -
	int counter = 12;

	int len = 0;
	len = comData[CURSOR_DATA]->decodeMsg(msg + counter);
	counter += len;

	len = comData[STATE_DATA]->decodeMsg(msg);
}
the first 3 bytes in the buffer are integers (general data such as - total len, msgID etc.) and I decode them in the 2nd function. afterwards I send the buffer into specific data classes that decode the relevant data they need using the fuction decodeMsg.

my questions are as follows:

1) why is the length of the buffer (decodeUnifiedMsgForServer) is always 0 and not equals to the length of the msg received from server? I know that the data is received from the server because it is logged correctly, but still when I check length of msg it equas 0 (first 'if' in second function) - any ideas?

2) when I remarked the line that calls the decodeMsg the application 'behaved' OK and client didn't exit its operation. I think that this error has something to do with memory leak. maybe if I understood the first question I could understand this one as well.

Thank you so much for your help,
Lenna
  #2 (permalink)  
Old 07-16-2005
lenna lenna is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 3
well, I don't know the answer to the first question but I probably found out the answer for the seconf one. the reason why the program got crazy is that since it's multi threading there's a situation where an event is raised which cause the system to freak out. I still haven't found why it freaks out but if I prevent the event to be raised than logging is OK and client application is not exit its operation.

I'll be happy though if someone could answer my first question above - why is the length of buffer = 0? could it be because I use memcpy in my encode/ decode buffer functions?

thanks again,
Lenna
  #3 (permalink)  
Old 05-25-2009
ogerassimov ogerassimov is offline
Registered User
  
 

Join Date: May 2009
Posts: 4
Use Debugger ...

Hello Lenna,

It is a complex application, and the problems could be at different places so use debugger to investigate the exact reason. Nevertheless I've made some code observations:

1. About "TCPClient::readSocketData"
if the remote side closes the socket for writing ( shutdown the socket ) or closes it.
then the function "recv" returns 0

see man recv
"The return value will be 0 when the peer has performed an orderly shutdown."

That method will exit at the second return because "br" will be 0 and the return value will be 0. So Any data at the buffer will be not touched at all.

I also recommend you to use errno and #include <errno.h> to check the error after each failed system call. I recomment you to check errors.

2. Have in mind that if you are testing both the client and the server at the same machine (localhost) exiting the client with unclosed socket causes the server to receive SIG_PIPE signal. So I recommend you to block that signal at the server.

3. The method "BrainControlComData::decodeMsg(char* decodeUnifiedMsgForServer)" has no idea about the buffer length.

char* is a pointer to a byte or byte sequence

I recomment you to change that declaration to

BrainControlComData::decodeMsg(const char* decodeUnifiedMsgForServer, size_t size )

That's why these lines looks strange:

if(strlen(decodeUnifiedMsgForServer) == 0)
{
char* error = "probably an error (see Q1 below)";
}


I suppose that decodeMsg simply does not get the buffer with correct length, And it does not make any checks about the correct length. It assumes that everything is OK, and when that is not true you get strange results.

The memcpy is not neccessary
int Web_lenOfClass;
// decode len of msg
memcpy(&Web_lenOfClass, msg, size);
int lenOfClass = ntohl(Web_lenOfClass);


You could replace it with:
int lenOfClass = ntohl( *(uint32_t*)msg );

and also
int msgID = ntohl( *(uint32_t*)(msg + MSG_ID_LOCATION ) );


4. If you have time I would recomend you to create unit tests ( test in isolation ) about each important class/methods. And to check them more carefully. More testing with more precise tests.


Best Regards
O.

Last edited by ogerassimov; 05-25-2009 at 01:03 PM..
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0