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);
}