It seems that MapViewOfFile increases the reference count of the file mapping kernel object.
Quoted from the MSDN description of MapViewOfFile:
Mapped views of a file mapping object maintain internal references to the object, and a file mapping object does not close until all references to it are released. Therefore, to fully close a file mapping object, an application must unmap all mapped views of the file mapping object by calling
UnmapViewOfFileand close the file mapping object handle by callingCloseHandle. These functions can be called in any order.
Also, from Windows via C/C++, 5th Edition:
The preceding code shows the "expected" method for manipulating memory-mapped files. However, what it does not show is that the system increments the usage counts of the file object and the file-mapping object when you call
MapViewOfFile...
Despite these, my actual test suggests the opposite. I'm using Visual Studio 2015 on Windows 10 64-bit. The test program is as follows:
#include <windows.h>
int main() {
HANDLE h = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 128, "test");
void* p_memory = MapViewOfFile(h, FILE_MAP_WRITE, 0, 0, 0);
CloseHandle(h);
h = OpenFileMappingA(FILE_MAP_WRITE, FALSE, "test");
DWORD dw = GetLastError(); // ERROR_FILE_NOT_FOUND
}
The OpenFileMapping call failed with the last error ERROR_FILE_NOT_FOUND. When I remove the CloseHandle call, everything would be fine. This implies that the CloseHandle call eliminates the last reference count of the file mapping kernel object and destroys it. This in turn implies that MapViewOfFile does not actually increases the reference count of the object.
I want to make sure what is going on, and what is the exact semantics of MapViewOfFile with respect to reference counting of the file mapping kernel object.