title: CreateFileMapping, MapViewOfFile, handle leaking c++
body: <p><strong>Background:</strong> I am trying to create a memory mapped file that can be accessed by multiple processes. In the below code I only put in the code that pertains to the question I currently have to make things simpler. According to msdn I should be able to create a filemap, map the view of the file and close the handle I received from CreateFileMapping and the MapViewOfFile will keep my FileMap alive. The FileMap should be still accessible until I UnmapViewOfFile.</p>
  
  <p><strong>MSDN:</strong> <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx" rel="nofollow" title="CreateFileMapping function">CreateFileMapping function</a></p>
  
  <blockquote>
    <p>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 UnmapViewOfFile and close the file mapping object handle by calling CloseHandle. These functions can be called in any order.</p>
  </blockquote>
  
  <p><strong>Problem:</strong> After successfully mapping the view of the file and then closing the handle received by CreateFileMapping the FileMap no longer exists (it should still exist) and my MemMapFileReader is able to create a new map with error of 0. (When it should be receiving error 183 'already exists')</p>
  
  <p><strong>Bad Solution:</strong> Not closing the handle allows it to be accessed by the MemMapFileReader program but causes a handle leak in the MemMapFileCreator because the handle is never closed until the process is closed.</p>
  
  <p><strong>Question:</strong> What am I missing or doing wrong?</p>
  
  <p>MemMapFileCreator</p>
  
  <pre><code>#include "stdafx.h"
  
  
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;iostream&gt;
  #define BUF_SIZE 256
  TCHAR szName[] = TEXT("MyFileMappingObject");
  TCHAR szMsg[] = TEXT("Message from first process.");
  
  int _tmain()
  {
  HANDLE hMapFile;
  LPCTSTR pBuf;
  
  hMapFile = CreateFileMapping(
      INVALID_HANDLE_VALUE,    // use paging file
      NULL,                    // default security
      PAGE_READWRITE,          // read/write access
      0,                       // maximum object size (high-order DWORD)
      BUF_SIZE,                // maximum object size (low-order DWORD)
      szName);                 // name of mapping object
  
  DWORD lastError = GetLastError();
  if (hMapFile == NULL)
  {
      _tprintf(TEXT("Could not create file mapping object (%d).\n"),
          GetLastError());
      std::cin.get();
      return 1;
  }
  pBuf = (LPTSTR)MapViewOfFile(hMapFile,   // handle to map object
      FILE_MAP_ALL_ACCESS, // read/write permission
      0,
      0,
      BUF_SIZE);
  
  if (pBuf == NULL)
  {
      _tprintf(TEXT("Could not map view of file (%d).\n"),
          GetLastError());
  
      CloseHandle(hMapFile);
  
      std::cin.get();
      return 1;
  }
  
  
  CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
  
  CloseHandle(hMapFile);
  
  _getch();
  
  
  UnmapViewOfFile(pBuf);
  return 0;
  }
  </code></pre>
  
  <p>MemMapFileReader</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;iostream&gt;
  #pragma comment(lib, "user32.lib")
  
  #define BUF_SIZE 256
  TCHAR szName[] = TEXT("MyFileMappingObject");
  
  int _tmain()
  {
  HANDLE hMapFile;
  LPCTSTR pBuf;
  
  hMapFile = CreateFileMapping(
      INVALID_HANDLE_VALUE,
      NULL,
      PAGE_READWRITE,   // read/write access
      0,
      BUF_SIZE,
      szName);               // name of mapping object
  DWORD lastError = GetLastError();
  if (hMapFile == NULL)
  {
      _tprintf(TEXT("Could not open file mapping object (%d).\n"),
          GetLastError());
      std::cin.get();
      return 1;
  }
  
  pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
      FILE_MAP_ALL_ACCESS,  // read/write permission
      0,
      0,
      BUF_SIZE);
  
  if (pBuf == NULL)
  {
      _tprintf(TEXT("Could not map view of file (%d).\n"),
          GetLastError());
  
      CloseHandle(hMapFile);
  
      std::cin.get();
      return 1;
  }
  
  MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
  
  UnmapViewOfFile(pBuf);
  
  CloseHandle(hMapFile);
  
  std::cin.get();
  return 0;
  }
  </code></pre>