Re: CreateFileMapping - Still Don't Get It!

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

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


Date: Mon, 24 May 2004 02:59:31 -0400

See below...
On Mon, 24 May 2004 02:59:09 GMT, "Bruce Kingsley" <brucek5@mindspring.com> wrote:

>All:
>
>I'm really having problems finding good documentation on shared memory using
>CreateFileMapping. So here's my questions that go beyond the online docs:
>
>1) When you close the handle returned by CreateFileMapping, is the memory
>file closed - and deleted? If yes, why can I still read it when another
>process calls OpenFileMapping after the first process that called
>CreateFileMapping closed it's handle and it's MapViewOfFile was closed as
>well?
****
When the LAST handle is closed, the file is freed. In your case, you have used
INVALID_HANDLE_VALUE so when the last handle is closed, the space on the backing store is
deleted.
*****
>
>2) Should the first process that called CreateFileMapping keep the handle
>open until the application closes?
*****
This is probably a good idea; you keep the handle open as long as you think someone needs
the mapping. If you close the handle, and it is the last handle, the file mapping
evaporates in a puff of greasy purple smoke. If it was backed by the swapfile, the
contents are consumed in that same puff of smoke.
*****
>
>3) If the memory file is still there, as my comment #1 demonstrates, then
>way don't I get an error when the first process once again calls
>CreateFileMapping with the same name. I would expect a "File Already Exists"
>error.
*****
When you create a file mapping (or semaphore, or mutex, or event, or job object, or
waitable timer, ...), if it already exists, you get a handle to the existing object. You
can check GetLastError, which will tell you if it was created new or you are getting a
handle to the existing one.
*****
>
>4) If CreateFileMapping function is called in the future with a larger size,
>MapViewOfFile returns a "Access Denied" error. If I re-boot the computer,
>then call CreateFileMapping and MapViewOfFile with a larger size I don't get
>an error.
*****
If you need a reboot, you are doing something truly bad. It means that you have a process
that is keeping the handle open. So you have deeper problems than worrying about this.
*****
>
>5) If CreateFileMapping creates a file in memory, and keeps it there until
>the operating system restarts, how do you free the memory for other uses
>(memory leak!!)? How can I enlarge the size of the file once the file was
>created with a smaller size?
*****
It does not "create the file in memory"...this is never an option. It creates a mapping,
which has no memory representation. You use MapViewOfFile to map the file into your
memory. Your file mapping may or may not be backed by the paging file. You free the memory
by closing the mapping. To extend the mapping, you have to have everyone that holds a
handle to it close their handles, so all handles to the mapping are closed, and then
create a new mapping with the new size., then tell all the other apps that they can open
the new mapping, although I think this limitation applies only when the mapping is backed
by the pagefile (I think you'll have to experiment a bit, but I think that it would
probably work if you had a real file backing it).
*****

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



Relevant Pages

  • Re: CreateFileMapping & MapViewOfFile
    ... I wrote a shared memory library for use with porting ... The mapping of the same name is almost immediately after ... >>was passed to CreateFileMapping()? ...
    (microsoft.public.win32.programmer.kernel)
  • CreateFileMapping - Still Dont Get It!
    ... I'm really having problems finding good documentation on shared memory using ... When you close the handle returned by CreateFileMapping, ... Should the first process that called CreateFileMapping keep the handle ... MapViewOfFile returns a "Access Denied" error. ...
    (microsoft.public.vc.mfc)
  • Re: sharing memory map between processes (same parent)
    ... do openand mmapto map a file into memory (madvise would also be used). ... Then I would like the other process to be able to access that mapping. ... it may be possible to make it so the first process never does ...
    (comp.unix.programmer)
  • Re: ten thousand small processes
    ... Stack needs to be executable for the current signal trampoline ... the use of malloc() that is causing your primary ... if there is any heap memory in use at all, no matter what you do, ... either directly, as a 4M page mapping (not used for user processes, ...
    (freebsd-performance)
  • [RFC 1v2/3] mm: iommu: An API to unify IOMMU, CPU and device memory management
    ... Its use would allow all of the IOMMU to VM, ... +This document covers how to use the Virtual Contiguous Memory Manager ... +fine-grained mapping attribute control. ...
    (Linux-Kernel)