Search code examples
c++windowsmemory-mapped-files

Is it safe to unmap a view after releasing the memory mapped file?


I've been working under the assumption that

  1. Create memory mapped file
  2. Create view
  3. Do stuff
  4. Unmap view
  5. Release file

However, a situation has come up where it would be much more convenient to unmap the view after releasing the file so swap steps 4 and 5. I tested it, no errors, all Ok. Still feels fishy though. I didn't see anything about this in microsofts documentation, does anybody know if this practice would be safe? I'm obviously not using the view in between releasing the file and unmapping.


Solution

  • According to MSDN it is allowed.

    These calls to CloseHandle succeed even when there are file views that are still open. However, leaving file views mapped causes memory leaks.

    I would still prefer unmapping views first if possible, just for the sake of clarity.