Boost C++ Libraries: Ticket #12947: Failed to use memory-mapped file to interprocess communications https://svn.boost.org/trac10/ticket/12947 <p> In first process we create and write the memory-mapped file through boost::iostreams::mapped_file_sink with boost::iostreams::mapped_file::readwrite flag. In seconde process we attempt to open the same memory-mapped file for read through boost::iostreams::mapped_file_source with boost::iostreams::mapped_file::readonly flag. The aptempt has failed for second process. Windows error code 32 (The process cannot access the file because it is being used by another process). In source code of iostream library we discover the next code that attempt to open file (mapped_file.cpp, void mapped_file_impl::open_file(param_type p)): </p> <p> DWORD dwDesiredAccess = </p> <blockquote> <p> readonly ? </p> <blockquote> <p> GENERIC_READ : (GENERIC_READ | GENERIC_WRITE); </p> </blockquote> </blockquote> <blockquote> <p> DWORD dwCreationDisposition = (p.new_file_size != 0 &amp;&amp; !readonly) ? </p> <blockquote> <p> CREATE_ALWAYS : OPEN_EXISTING; </p> </blockquote> <p> DWORD dwFlagsandAttributes = </p> <blockquote> <p> readonly ? </p> <blockquote> <p> FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_TEMPORARY; </p> </blockquote> </blockquote> <p> handle_ = p.path.is_wide() ? </p> <blockquote> <p> ::CreateFileW( </p> <blockquote> <p> p.path.c_wstr(), dwDesiredAccess, FILE_SHARE_READ, NULL, dwCreationDisposition, dwFlagsandAttributes, NULL ) : </p> </blockquote> <p> ::CreateFileA( </p> <blockquote> <p> p.path.c_str(), dwDesiredAccess, FILE_SHARE_READ, NULL, dwCreationDisposition, dwFlagsandAttributes, NULL ); </p> </blockquote> </blockquote> <p> if (handle_ == INVALID_HANDLE_VALUE) </p> <blockquote> <p> cleanup_and_throw("failed opening file"); </p> </blockquote> </blockquote> <p> In both cases (opening the file for write of for read) the dwShareMode flag has FILE_SHARE_READ value. This is wrong! Using the rules that dуscraybed on page <a class="ext-link" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363874(v=vs.85).aspx"><span class="icon">​</span>https://msdn.microsoft.com/en-us/library/windows/desktop/aa363874(v=vs.85).aspx</a> we corrected the code of function: </p> <p> DWORD dwDesiredAccess = </p> <blockquote> <p> readonly ? </p> <blockquote> <p> GENERIC_READ : (GENERIC_READ | GENERIC_WRITE); </p> </blockquote> </blockquote> <blockquote> <p> DWORD dwCreationDisposition = (p.new_file_size != 0 &amp;&amp; !readonly) ? </p> <blockquote> <p> CREATE_ALWAYS : OPEN_EXISTING; </p> </blockquote> <p> DWORD dwFlagsandAttributes = </p> <blockquote> <p> readonly ? </p> <blockquote> <p> FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_TEMPORARY; </p> </blockquote> </blockquote> <p> handle_ = p.path.is_wide() ? </p> <blockquote> <p> ::CreateFileW( </p> <blockquote> <p> p.path.c_wstr(), dwDesiredAccess, !readonly ? FILE_SHARE_READ : FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, dwCreationDisposition, dwFlagsandAttributes, NULL ) : </p> </blockquote> <p> ::CreateFileA( </p> <blockquote> <p> p.path.c_str(), dwDesiredAccess, !readonly ? FILE_SHARE_READ : FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, dwCreationDisposition, dwFlagsandAttributes, NULL ); </p> </blockquote> </blockquote> <p> if (handle_ == INVALID_HANDLE_VALUE) </p> <blockquote> <p> cleanup_and_throw("failed opening file"); </p> </blockquote> </blockquote> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12947 Trac 1.4.3 Valeriy N. Sifurov <vsifurov@…> Tue, 04 Apr 2017 14:47:46 GMT attachment set https://svn.boost.org/trac10/ticket/12947 https://svn.boost.org/trac10/ticket/12947 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">mapped_file.hpp</span> </li> </ul> <p> Corrected code </p> Ticket Mateusz Loskot Thu, 08 Feb 2018 17:59:56 GMT <link>https://svn.boost.org/trac10/ticket/12947#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12947#comment:1</guid> <description> <p> Is there any chance this fix could be accepted? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 12 Apr 2018 08:31:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12947#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12947#comment:2</guid> <description> <p> Yeah, unless there is some other way to open the same memory mapped file from two processes, this seems to prevent that use case :-(. Not everyone may want to use boost::interprocess! </p> </description> <category>Ticket</category> </item> </channel> </rss>