Tech Support > Microsoft Windows > Development Resources > Process Memory Usage is increasing continously and then crashes
Process Memory Usage is increasing continously and then crashes
Posted by Gopal Paripally on February 4th, 2004


I am using CreateFileMapping, MapViewOfFile to write the data and then
using MapViewofFile to get the data and then writing into file on the
harddisk. Each time I call the writeFile, the memUsage is increasing
by the size of the file that am writing (about 250k). Since it is
going to be in a loop for about 1000 times, the system is running out
of resources. I just don't understand on what am doing wrong with the
following code. I tried with several flags for CreateFileMapping and
writeFile. none of them help. I am calling UnMapViewofFile at the end.
(I tried changing moving UnMapViewOfFile as soon as am done with
writing file, but that didn't help).The sample code is here. Actually
the code is in two layers UI & DLL, but simplicity I moved every
thing to UI Layer.

//*************************

//In OnInitDialog of the Dialogbased App:

m_hMMFile = CreateFileMapping((HANDLE)0xffffffff,NULL,PAGE_REA DWRITE,0,256*1024,NULL);
m_pMappedFile = (LPBYTE)MapViewOfFile(m_hMMFile,FILE_MAP_ALL_ACCES S,0,0,0);

void CGopalDialog::OnButton1()
{
//OnButton1 (write to hard disk, at the end of this operation my
process mem usage increases by 250k).

//Write to Memory Mapped File.
char bigBuffer[256*1000];
int index1 = 0;
while(index1 <256*1000)
{
strncpy(&bigBuffer[index1],"This is to test by Gopal",20);
index1 = index1+20;
}
hmemcpy(m_pMappedFile, bigBuffer, 256*1000-10); //also tried
Copy/MoveMemory


//Extract from memory mapped file and then write to disk.
//This code is actually in the DLL, but I moved to UI OnButton1 for
simplicity
LPVOID mmPtr;
mmPtr = ::MapViewOfFile((HANDLE) m_hMMFile, FILE_MAP_READ, 0, 0, 0);

char buffer[50];
m_tempVariable++; //temp file index.
sprintf( buffer, _T("C:\\temp\\PolycomMemoryLeak%d"), m_tempVariable
);
//Create a temporary file.
HANDLE tempFileHandle = CreateFile(buffer,
GENERIC_READ |

GENERIC_WRITE,FILE_SHARE_READ , NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY,NULL);

DWORD dwBytesWriten;
//Write to temporary file.
BOOL bWriteFileSuccess = WriteFile(tempFileHandle,

mmPtr,256*1000-10,&dwBytesWriten, NULL);

CloseHandle(tempFileHandle);

}//End of Dialog's OnButton1

CDialog::OnClose()
{
OnCancel of the dialog: I have the following code.
UnmapViewOfFile( m_pMappedFile );
CloseHandle(m_hMMFile);
}

Posted by Gil Hamilton on February 5th, 2004


paripally@yahoo.com (Gopal Paripally) wrote in message news:<cdce8b13.0402041317.34d66d16@posting.google. com>...
You're creating a new file mapping each time you go through OnButton1:

I don't see that you're freeing it anywhere -- there should be an
UnmapViewOfFile to go with it each time through.

BTW, this code looks very confused; exactly what are you trying to do?
You appear to be "going around the block to get next door". First,
you allocate file mapping m_pMappedFile, then you allocate bigBuffer
on the stack, then after copying bigBuffer to m_pMappedFile, you
allocate a third buffer with mmPtr, then you write it to file
tempFileHandle. Why don't you just create a file mapping using
tempFileHandle and construct your data directly in that memory? Or,
at least, just write to tempFileHandle directly from whatever memory
has the data?

- GH

Posted by Gopal Paripally on February 5th, 2004


Thanks for the reply. I fixed it as soon as I submitted my request.
LOng story behind on why am donig that.

Thanks any way.
GP
gil_hamilton@hotmail.com (Gil Hamilton) wrote in message news:<df28a668.0402051041.91885d8@posting.google.c om>...


Similar Posts