[Boost-bugs] [Boost C++ Libraries] #2996: [iostreams] Can't open mapped_file data file for read/write access on Windows

Subject: [Boost-bugs] [Boost C++ Libraries] #2996: [iostreams] Can't open mapped_file data file for read/write access on Windows
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-05-05 16:29:58

#2996: [iostreams] Can't open mapped_file data file for read/write access on
 Reporter: Allen Cronce <acronce_at_[hidden]> | Owner: turkanis
     Type: Bugs | Status: new
Milestone: Boost 1.39.0 | Component: iostreams
  Version: Boost 1.38.0 | Severity: Problem
 Keywords: |
 I see access errors when trying to use mapped_file open a data file for
 reading and writing under Windows (built with VS2005). The same code works
 fine under Mac OS X, which is posix, of course.

 Note that I originally found this problem under boost 1.35, but it is
 still present in the just released 1.39 version. Unfortunately it looks
 like trac has not been updated, so I cannot set the "version" to 1.38, or
 the "milestone" to 1.40.

 Here's a code sample that works on Mac OS X and fails under Windows:


 Test boost iostream mapped_file.
 static int BoostMappedFileTest(void)
         int result = 0;

         std::cout << "\nRunning BoostMappedFileTest..." << std::endl;

         // Initialize our test directory
         fs::path testDirPath( "BoostMappedFileTest" );

         // Initialize a test file buffer with some data
         const size_t kTestFileSize = 4096;
         boost::scoped_array<char> fileBuffer( new char[kTestFileSize] );
         char* pCur = fileBuffer.get();
         char curChar = 0;
         for( size_t i = 0; i < kTestFileSize; ++i )
                 *pCur++ = curChar++;

         // Write the data to a test file. Be sure to explicitly close it.
         fs::path testFilePath( testDirPath / "test_mapped_file" );
         boost::filesystem::ofstream testFile( testFilePath, ios::trunc |
 ios::binary );
         testFile.write(fileBuffer.get(), kTestFileSize);

         // Try and open the test file as a memory mapped file.
         string testFilePathStr = testFilePath.file_string();
         mapped_file mappedFile(testFilePathStr);

         // We never get here under Windows. The file fails to
         // open with an access error.

         // Compare the mapped file data to our original buffer
         const char* mmConstData = mappedFile.const_data();
         bool compareSucceeded = (0 == memcmp(fileBuffer.get(),
 mmConstData, kTestFileSize));
         if (!compareSucceeded)
                 throw runtime_error("The boost memory mapped file data
 doesn't match our buffer!");

         return result;


 The problem seems to come from passing GENERIC_ALL as the
 "dwDesiredAccess" parameter to the CreateFileA call. If I change the
 implementation to pass GENERIC_READ | GENERIC_WRITE, then it works in my
 test scenario. Please note that I haven't tried pointing the current code
 at an actual executable to see if it works with GENERIC_ALL.

 If you assume a strict interpretation of the mapped_file API, the mode of
 BOOST_IOS::in would map to GENERIC_READ in the Windows world, and
 BOOST_IOS::out would map to GENERIC_WRITE. There is no corresponding
 "execute" flag that I can see in this API.

 I've attached a diff created from the boost 1.39 release that fixes the

 My email address (in case my trac preferences doesn't work) is

Ticket URL: <https://svn.boost.org/trac/boost/ticket/2996>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:00 UTC