Re: CreateFileMapping - Still Don't Get It!
From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/24/04
- Next message: Joseph M. Newcomer: "Re: C++ Object Pointers"
- Previous message: Joseph M. Newcomer: "Re: Is there a Visual C++ Net only product ???"
- In reply to: Bruce Kingsley: "CreateFileMapping - Still Don't Get It!"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Joseph M. Newcomer: "Re: C++ Object Pointers"
- Previous message: Joseph M. Newcomer: "Re: Is there a Visual C++ Net only product ???"
- In reply to: Bruce Kingsley: "CreateFileMapping - Still Don't Get It!"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|