Posted on 1999-01-16
Last Modified: 2010-04-15
When do we have to use OpenSemaphore()???  And what will happens if my application does not use UnmapFile Viewing() and CloseHandle() atfer the application had finished? My application was able to access the Shared memory many times... but to a certain limit of continuous accessing the shared memory(about 15 mins) without using the closhandle and unmapFile Viewing(). What could be the main cause? Thanx!
Question by:Meng

Author Comment

ID: 1258155
Please send your answers to my office account at   Thanx a billion!
LVL 11

Accepted Solution

alexo earned 50 total points
ID: 1258156
>> When do we have to use OpenSemaphore()?

The OpenSemaphore function enables multiple processes to open handles of the same semaphore object.  The function succeeds only if some process has already created the semaphore by using the CreateSemaphore function.

>> what will happens if my application does not use UnmapFileViewing() and CloseHandle() atfer the application had finished?

UnmapViewOfFile() is needed because although an application may close the file handle used to create a file mapping object, the system holds the corresponding file open until the last view of the file is unmapped.

When a process exits, all of the object handles opened by the process are closed.  Thus, technically it is not strictly necessary to use CloseHandle() but it is a good programming practice to do it anyway.

From KB article Q125713:

Necessity of Unmapping All Views of a Mapped File
Windows maintains an internal handle to a file mapping object for each view of that object, whether created by MapViewOfFile() or MapViewOfFileEx().  This internal handle is kept in addition to the handle returned by CreateFileMapping().  The internal handle is not closed until the view associated with the handle is unmapped by calling UnmapViewOfFile(). To completely close a file mapping object requires that all handles for the object, including internal handles, be closed.  Thus, to close a file mapping object, all views of that object must be unmapped, and the handle returned by CreateFileMapping() must be closed.
Extant unmapped views of a file mapping object will NOT cause a CloseHandle() on the object's handle to fail.  In other words, when your handle to the object is closed successfully, it is not necessarily true that all views have been unmapped, so the file mapping object has not necessarily been freed.
Failure to properly unmap all views of the object and to close the handle to the object will cause leaks in the application's paged pool, nonpaged pool, virtual bytes, and also in the system wide committed bytes.


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question