am trying to develop an application to transfer a folder of files
using sockets. I have been using a loop to transfer all the files.
there are many memory leakage errors found in debugging. how can i
solve this?

the output is differing. and I am unable to find the exactly where
is the error in the code.

the code for sending is like this : in which i am sending the contents
in the order
file size, filename size, file name, file contents

void Sender::SendData(void)
{
CString findFilePath=filePathInformation+"\\*.*";

bWorking=finder.FindFile(_T((LPCTSTR)findFilePath));

while (bWorking)
{

bWorking = finder.FindNextFile();
CString findFileName(finder.GetFileName());
if(findFileName.Compare(".") == 0 ||
findFileName.Compare("..") == 0)
{
//CloseSocket();
continue;
}
int returnValue=SetAddress();
if(returnValue==0)
{
CString sendFilePath=finder.GetFilePath();
FILE *filePointer;
//int returnedValue;
if( (filePointer = fopen(sendFilePath, "rb" ))
== NULL )
{
cout<<endl<<"File open error";
CloseSocket();
return;
}
else
{
char *fileContentBuffer;
unsigned int bytesRead,fileNameLength;
long fileSize;
//long allocReqNum;
//first send the file size details

fseek(filePointer, 0, SEEK_END); //
Move File Pointer to EOF
fileSize=ftell(filePointer); // Get
position of File Pointer
rewind(filePointer); // Move File
Pointer back to beginning of
file
fileContentBuffer = (char
*)calloc(32,sizeof(long));

//returnedValue=_CrtIsMemoryBlock(fileContentBuffer, 32,
&allocReqNum, NULL, NULL);
ltoa( fileSize, fileContentBuffer,10 );
int returnValue =
send(mySocket,fileContentBuffer,32,0);
if(returnValue == SOCKET_ERROR)
{
cout<<endl<<"Error in Sending
File Length";
CloseSocket();
return;
}
else
cout<<endl<<"File Size Sent
Successfully : "<<fileSize;
//long
valueReturned=_CrtSetBreakAlloc(allocReqNum);
//delete fileContentBuffer;
free(fileContentBuffer);

//send file name length

fileNameLength=findFileName.GetLength();
fileContentBuffer = (char
*)calloc(32,sizeof(int));
char *returnChar=itoa( fileNameLength,
fileContentBuffer, 10 );
returnValue =
send(mySocket,fileContentBuffer,32,0);
if(returnValue == SOCKET_ERROR)
{
cout<<endl<<"Error in Sending
File Details";
CloseSocket();
return;
}
else
cout<<endl<<"File Length Sent
Successfully : "<<fileNameLength;
free(fileContentBuffer);
//delete fileContentBuffer;
//Send the file name
char *temporaryBuffer;
fileContentBuffer = (char
*)calloc(255,sizeof(char));

temporaryBuffer=findFileName.GetBuffer(fileNameLength);

strcpy(fileContentBuffer,temporaryBuffer);
returnValue =
send(mySocket,fileContentBuffer,fileNameLength,0);
if(returnValue == SOCKET_ERROR)
{
cout<<endl<<"Error in Sending
in File Name";
CloseSocket();
return;
}
else
cout<<endl<<"File Name Sent
Successfully : "<<temporaryBuffer;
free(fileContentBuffer);
//delete fileContentBuffer;
//sending the file
fileContentBuffer = (char
*)calloc(fileSize,sizeof(char));

bytesRead=fread(fileContentBuffer,sizeof(char),fileSize,filePointer);
returnValue =
send(mySocket,fileContentBuffer,fileSize,0);
if(returnValue == SOCKET_ERROR)
{
cout<<endl<<"Error in Sending
File Contents";
CloseSocket();
return;
}
else
cout<<endl<<"File Sent
Successfully "<< returnValue<<" bytes";
//delete fileContentBuffer;
free(fileContentBuffer);
}
//returnedValue=_CrtDumpMemoryLeaks();
fclose(filePointer);
CloseSocket();
}
else
{
cout<<endl<<"Error in connecting";
CloseSocket();
return;
}
}

}
could u please help regarding this.
that would be greatful to me,
sirisha.

Re: memory leakage in vc++ 6.0 by Scott

Scott
Tue Jun 06 10:00:21 CDT 2006

sirisha wrote:
> am trying to develop an application to transfer a folder of files
> using sockets. I have been using a loop to transfer all the files.
> there are many memory leakage errors found in debugging. how can i
> solve this?

None of your error returns free the memory you allocated. Why don't you
just eliminate all the calloc's and define a couple of fixed-size
buffers at the begining of your function? There is no need for dynamic
memory allocation in what you are doing.

--
Scott McPhillips [VC++ MVP]


Re: memory leakage in vc++ 6.0 by Ulrich

Ulrich
Wed Jun 07 02:16:12 CDT 2006

sirisha wrote:
> am trying to develop an application to transfer a folder of files
> using sockets.

rsync and possibly FTP already do that, why reinvent the wheel?

> bWorking=finder.FindFile(_T((LPCTSTR)findFilePath));

What the hell are you doing here? You shouldn't be casting things around
like this and the _T macro should only be used in combination with string
literals....

> long fileSize;

long is not long enough. Further, you could use the win32 API to get the
size of the file in a much more elegant way.

> fileContentBuffer = (char*)calloc(32,sizeof(long));

Use static_cast.

> if(returnValue == SOCKET_ERROR)
> {
> cout<<endl<<"Error in Sending File Length";
> CloseSocket();
> return;
> }
> else
> cout<<endl<<"File Size Sent Successfully : "<<fileSize;
> free(fileContentBuffer);

In case of an error, you fail to release allocated storage. You should
research the RAII idiom of C++ (or use std::vector right away).

Uli