Boost C++ Libraries: Ticket #12182: boost::asio async_send corrupts memory on Visual Studio 2015 x64 build https://svn.boost.org/trac10/ticket/12182 <p> Hi, I've got unexpected problem on Windows using Visual Studio 2015 x64. Problem occurs only in x64 Debug build on Visual Studio 2015. All works fine in Win32 build, Linux and Android platforms also. Seems x64 Release build also works fine. Brief description: </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: </p> <div class="wiki-code"><div class="code"><pre><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">async_write</span><span class="p">(</span><span class="o">*</span><span class="n">m_socket</span><span class="p">,</span> <span class="n">buffers</span><span class="p">,</span> <span class="n">make_write_handler</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">base_connection</span><span class="o">::</span><span class="n">on_write</span><span class="p">,</span> <span class="n">self</span><span class="p">(),</span> <span class="n">_1</span><span class="p">,</span> <span class="n">_2</span><span class="p">)));</span> </pre></div></div></div><p> Line 69 in <a class="ext-link" href="https://github.com/qmule/libed2k/blob/kad/src/base_connection.cpp"><span class="icon">​</span>https://github.com/qmule/libed2k/blob/kad/src/base_connection.cpp</a> I've got rewrited at least 4 bytes in member of peer_connection class. So, I set breakpoint on data write for class member and get stack when it happens. From stack I see memory corruption occurred on creation object in file win_iocp_socket_service_base.hpp:226 </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: </p> <div class="wiki-code"><div class="code"><pre><span class="c1">// Allocate and construct an operation to wrap the handler.</span> <span class="k">typedef</span> <span class="n">win_iocp_socket_send_op</span><span class="o">&lt;</span><span class="n">ConstBufferSequence</span><span class="p">,</span> <span class="n">Handler</span><span class="o">&gt;</span> <span class="n">op</span><span class="p">;</span> <span class="k">typename</span> <span class="n">op</span><span class="o">::</span><span class="n">ptr</span> <span class="n">p</span> <span class="o">=</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">addressof</span><span class="p">(</span><span class="n">handler</span><span class="p">),</span> <span class="n">boost_asio_handler_alloc_helpers</span><span class="o">::</span><span class="n">allocate</span><span class="p">(</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">op</span><span class="p">),</span> <span class="n">handler</span><span class="p">),</span> <span class="mi">0</span> <span class="p">};</span> <span class="n">p</span><span class="p">.</span><span class="n">p</span> <span class="o">=</span> <span class="k">new</span> <span class="p">(</span><span class="n">p</span><span class="p">.</span><span class="n">v</span><span class="p">)</span> <span class="n">op</span><span class="p">(</span><span class="n">impl</span><span class="p">.</span><span class="n">cancel_token_</span><span class="p">,</span> <span class="n">buffers</span><span class="p">,</span> <span class="n">handler</span><span class="p">);</span> </pre></div></div></div><p> Actually seems problem in boost::asio allocation helper, entry point in file handler_alloc_helpers.hpp:31 - since when I defined BOOST_ASIO_DISABLE_HANDLER_HOOKS to activate simple new for allocation problem was fixed: </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: </p> <div class="wiki-code"><div class="code"><pre><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">Handler</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="kt">void</span><span class="o">*</span> <span class="n">allocate</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="kt">size_t</span> <span class="n">s</span><span class="p">,</span> <span class="n">Handler</span><span class="o">&amp;</span> <span class="n">h</span><span class="p">)</span> <span class="p">{</span> <span class="cp">#if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS)</span> <span class="k">return</span> <span class="o">::</span><span class="k">operator</span> <span class="k">new</span><span class="p">(</span><span class="n">s</span><span class="p">);</span> <span class="cp">#else</span> <span class="k">using</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">asio_handler_allocate</span><span class="p">;</span> <span class="k">return</span> <span class="nf">asio_handler_allocate</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">addressof</span><span class="p">(</span><span class="n">h</span><span class="p">));</span> <span class="cp">#endif</span> <span class="p">}</span> </pre></div></div></div><p> Full stack on breakpoint on memory: </p> <pre class="wiki">conn.exe!boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt;::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt;(const boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt; &amp; other) Line 188 C++ conn.exe!boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt;::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt;(boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp;,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &amp;&amp; other) Line 165 C++ conn.exe!boost::asio::detail::win_iocp_socket_send_op&lt;boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt;,boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &gt;::win_iocp_socket_send_op&lt;boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt;,boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &gt;(std::weak_ptr&lt;void&gt; cancel_token, const boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt; &amp; buffers, boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp;,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &amp; handler) Line 49 C++ conn.exe!boost::asio::detail::win_iocp_socket_service_base::async_send&lt;boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt;,boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &gt;(boost::asio::detail::win_iocp_socket_service_base::base_implementation_type &amp; impl, const boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt; &amp; buffers, int flags, boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp;,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &amp; handler) Line 226 C++ conn.exe!boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt;::async_send&lt;boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt;,boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &gt;(boost::asio::detail::win_iocp_socket_service&lt;boost::asio::ip::tcp&gt;::implementation_type &amp; impl, const boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt; &amp; buffers, int flags, boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp;,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &amp;&amp; handler) Line 332 C++ conn.exe!boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;::async_write_some&lt;boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt;,boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &gt;(const boost::asio::detail::consuming_buffers&lt;boost::asio::const_buffer,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &gt; &amp; buffers, boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp;,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt; &amp;&amp; handler) Line 733 C++ conn.exe!boost::asio::detail::write_op&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,boost::asio::detail::transfer_all_t,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt;::operator()(const boost::system::error_code &amp; ec, unsigned __int64 bytes_transferred, int start) Line 183 C++ conn.exe!boost::asio::async_write&lt;boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt;,std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt;,libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp; __ptr64,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &gt;(boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp,boost::asio::stream_socket_service&lt;boost::asio::ip::tcp&gt; &gt; &amp; s, const std::list&lt;boost::asio::const_buffer,std::allocator&lt;boost::asio::const_buffer&gt; &gt; &amp; buffers, libed2k::base_connection::allocating_handler&lt;boost::_bi::bind_t&lt;void,boost::_mfi::mf2&lt;void,libed2k::base_connection,boost::system::error_code const &amp;,unsigned __int64&gt;,boost::_bi::list3&lt;boost::_bi::value&lt;boost::intrusive_ptr&lt;libed2k::base_connection&gt; &gt;,boost::arg&lt;1&gt;,boost::arg&lt;2&gt; &gt; &gt;,300&gt; &amp;&amp; handler) Line 624 C++ conn.exe!libed2k::base_connection::do_write(int quota) Line 69 C++ conn.exe!libed2k::peer_connection::do_write(int __formal) Line 492 C++ conn.exe!libed2k::base_connection::write_message(const std::pair&lt;libed2k::libed2k_header,std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; &gt; &amp; msg) Line 78 C++ conn.exe!libed2k::base_connection::write_struct&lt;libed2k::client_hello&gt;(const libed2k::client_hello &amp; t) Line 75 C++ conn.exe!libed2k::peer_connection::write_struct&lt;libed2k::client_hello&gt;(libed2k::client_hello &amp; t) Line 279 C++ conn.exe!libed2k::peer_connection::write_hello() Line 1667 C++ conn.exe!libed2k::peer_connection::on_connect(const boost::system::error_code &amp; e) Line 1082 C++ </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12182 Trac 1.4.3