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




Thread: memory leak?
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #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 02:03 PM..