Re: CreateFileMapping Question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/21/04


Date: Fri, 21 May 2004 03:23:04 -0400

It is safe to assume that memory management is going on correctly under the mapping.
WHether or not the same memory is used is not particularly relevant; but the memory that
had been used will be available for other purposes. Maybe it will be reused for the next
mapping, maybe not. This is unpredictable.

Note that the file mappings may be in different virtual addresses, so the
SampeTransferType cannot use pointers, since pointers set by one process will be nonsense
to the other process, unless you use __based pointers (that's a long discussion, so if you
aren't using pointers, I won't bore you with it. If you are, follow up and I'll explain).

Since each process handles the mapping independently, it doesn't matter too much. Example:
if process A gets a mapping at 0x100000 and closes it, and then creates another mapping,
it might be at 0x100000, or it might be at 0x200000, but if it is at 0x200000, then the
memory at 0x100000 is now available for other purposes. I believe it tries to reuse memory
as efficiently as possible, but there are no guarantees that the addresses will
necessarily be the same (e.g., the mapping might have fit at 0x100000 before, but the next
mapping might be to a larger version of the file, which no longer fits in that space).
Whether or not the mapping is held open by another process is irrelevant at this point,
because the mapping is local to process A. Process B might still have the mapping open on
ITS address space 0x100000, process A opens the mapping, and gets 0x200000, but after B
closes the mapping and re-opens it, it might still fit at 0x100000 in B, so it will appear
there.

Ultimately, you should not see, on the average, a memory leak.
                                        joe

On Fri, 21 May 2004 02:38:29 GMT, "Bruce Kingsley" <brucek5@mindspring.com> wrote:

>All:
>
>I have a question about the use of CreateFileMapping functions. See example
>below...
>
>First Process:
>
> HANDLE hmem = ::CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL,
>PAGE_READWRITE, 0, FileSize, FM_SCOPEMAPFILENAME);
>
> FileBuffer = (SampleTransferType*)::MapViewOfFile(hmem, FILE_MAP_WRITE, 0,
>0, FileSize);
>
>// Fill FileBuffer here....
>
> ::UnmapViewOfFile(FileBuffer);
> ::CloseHandle(hmem);
>
>
>Second Process:
>
>
> HANDLE hmem = ::OpenFileMapping(FILE_MAP_WRITE, FALSE, FM_ADCMAPFILENAME);
>
> FileBuffer = (SampleTransferType*)::MapViewOfFile(hmem, FILE_MAP_WRITE, 0,
>0, FileSize);
>
>// FileBuffer is read here...
>
> ::UnmapViewOfFile(FileBuffer);
> ::CloseHandle(hmem);
>
>
>All works, but I'm concerned about memory leaks. This code is repeatably
>used and I wonder what frees the memory used by these functions? The first
>process closes all it's handles before the second process opens the memory
>file, so I know the memory is still be held. When the first process is
>called again and calls CreateFileMapping with the same name, does it re-use
>the same memory, or does it allocate even more memory?

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages