• C

Semaphore

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!
MengAsked:
Who is Participating?
 
alexoConnect With a Mentor Commented:
>> 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.

0
 
MengAuthor Commented:
Please send your answers to my office account at ho_cm@gitpl.com.sg   Thanx a billion!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.