Boost C++ Libraries: Ticket #9597: "boost::iostreams::file_descriptor" doesn't close Windows low-level file descriptors properly when specifying "file_descriptor_flags::close_handle" https://svn.boost.org/trac10/ticket/9597 <p> When opening a file on Windows using <code>boost::iostreams::file_descriptor</code>, <code>boost::iostreams::file_descriptor_sink</code> or <code>boost::iostreams::file_descriptor_source</code> in combination with a low-level file descriptor and when specifying <code>file_descriptor_flags::close_handle</code>, then the file descriptor isn't closed properly on close/exit. </p> <p> Using one of the mentioned methods, the low-level file descriptor is converted to a Windows handle using the API call <code>_get_osfhandle</code> <a class="new ticket" href="https://svn.boost.org/trac10/ticket/9597#point1" title="#9597: Bugs: &#34;boost::iostreams::file_descriptor&#34; doesn't close Windows low-level ... (new)">(1)</a>: </p> <div class="wiki-code"><div class="code"><pre><span class="kt">void</span> <span class="n">file_descriptor_impl</span><span class="o">::</span><span class="n">open</span><span class="p">(</span><span class="kt">int</span> <span class="n">fd</span><span class="p">,</span> <span class="n">flags</span> <span class="n">f</span><span class="p">)</span> <span class="p">{</span> <span class="n">open</span><span class="p">(</span><span class="k">reinterpret_cast</span><span class="o">&lt;</span><span class="n">file_handle</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_get_osfhandle</span><span class="p">(</span><span class="n">fd</span><span class="p">)),</span> <span class="n">f</span><span class="p">);</span> <span class="p">}</span> </pre></div></div><p> When closing, the file handle is closed using <code>CloseHandle</code>: </p> <div class="wiki-code"><div class="code"><pre><span class="kt">void</span> <span class="n">file_descriptor_impl</span><span class="o">::</span><span class="n">close_impl</span><span class="p">(</span><span class="kt">bool</span> <span class="n">close_flag</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">throw_</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">handle_</span> <span class="o">!=</span> <span class="n">invalid_handle</span><span class="p">())</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">close_flag</span><span class="p">)</span> <span class="p">{</span> <span class="kt">bool</span> <span class="n">success</span> <span class="o">=</span> <span class="err">#</span><span class="n">ifdef</span> <span class="n">BOOST_IOSTREAMS_WINDOWS</span> <span class="o">::</span><span class="n">CloseHandle</span><span class="p">(</span><span class="n">handle_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">;</span> <span class="cp">#else</span> <span class="n">BOOST_IOSTREAMS_FD_CLOSE</span><span class="p">(</span><span class="n">handle_</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="cp">#endif</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">success</span> <span class="o">&amp;&amp;</span> <span class="n">throw_</span><span class="p">)</span> <span class="n">throw_system_failure</span><span class="p">(</span><span class="s">&quot;failed closing file&quot;</span><span class="p">);</span> <span class="p">}</span> <span class="n">handle_</span> <span class="o">=</span> <span class="n">invalid_handle</span><span class="p">();</span> <span class="n">flags_</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </pre></div></div><p> According to <a class="new ticket" href="https://svn.boost.org/trac10/ticket/9597#point1" title="#9597: Bugs: &#34;boost::iostreams::file_descriptor&#34; doesn't close Windows low-level ... (new)">(1)</a> the API call <code>_close</code> <a class="new ticket" href="https://svn.boost.org/trac10/ticket/9597#point2" title="#9597: Bugs: &#34;boost::iostreams::file_descriptor&#34; doesn't close Windows low-level ... (new)">(2)</a> shall be used to close a low-level file descriptor. Practical tests shew that every time a file is opened and closed using one of the mentioned methods, Windows (XP) assigns a new file descriptor with an increasing value to it, unless the program runs out of available file handles. Manually closing the file by specifying <code>file_descriptor_flags::close_handle</code> and calling <code>_close</code> solves this issue. </p> <p> <span class="wikianchor" id="point1">(1)</span> <a class="ext-link" href="http://msdn.microsoft.com/en-us/library/ks2530z6.aspx"><span class="icon">​</span>http://msdn.microsoft.com/en-us/library/ks2530z6.aspx</a> <br /> <span class="wikianchor" id="point2">(2)</span> <a class="ext-link" href="http://msdn.microsoft.com/en-us/library/5fzwd5ss.aspx"><span class="icon">​</span>http://msdn.microsoft.com/en-us/library/5fzwd5ss.aspx</a> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9597 Trac 1.4.3