Boost C++ Libraries: Ticket #13245: Coroutines2: Crashes Visual Studio when attached with a debugger (on Windows x86) https://svn.boost.org/trac10/ticket/13245 <p> Hi, </p> <p> In my application I'm using coroutine2 to generate some objects which I have to decode from a stream. These objects are generated using coroutines. My problem is that as soon as I reach the end of the stream and would theoretically throw std::ios_base::failure my application crashes under certain conditions. </p> <p> The function providing this feature is implemented in C++, exported as a C function and called from C#. This all happens on a 32bit process on Windows 10 x64. Unfortunately it only reliably crashes when I start my test from C# in debugging mode WITHOUT the native debugger attached. As soon as I attach the native debugger everything works like expected. </p> <p> Here is a small test application to reproduce this issue: </p> <p> Api.h </p> <pre class="wiki">#pragma once extern "C" __declspec(dllexport) int __cdecl test(); </pre><p> Api.cpp </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;sstream&gt; #include "Api.h" #define BOOST_COROUTINES2_SOURCE #include &lt;boost/coroutine2/coroutine.hpp&gt; int test() { using coro_t = boost::coroutines2::coroutine&lt;bool&gt;; coro_t::pull_type source([](coro_t::push_type&amp; yield) { std::vector&lt;char&gt; buffer(200300, 0); std::stringstream stream; stream.write(buffer.data(), buffer.size()); stream.exceptions(std::ios_base::eofbit | std::ios_base::badbit | std::ios_base::failbit); try { std::vector&lt;char&gt; dest(100100, 0); while (stream.good() &amp;&amp; !stream.eof()) { stream.read(&amp;dest[0], dest.size()); std::cerr &lt;&lt; "CORO: read: " &lt;&lt; stream.gcount() &lt;&lt; std::endl; } } catch (const std::exception&amp; ex) { std::cerr &lt;&lt; "CORO: caught ex: " &lt;&lt; ex.what() &lt;&lt; std::endl; } catch (...) { std::cerr &lt;&lt; "CORO: caught unknown exception." &lt;&lt; std::endl; } }); std::cout &lt;&lt; "SUCCESS" &lt;&lt; std::endl; return 0; } </pre><p> c#: </p> <pre class="wiki">using System; using System.Runtime.InteropServices; namespace CoroutinesTest { class Program { [DllImport("ConsoleApplication1.dll", EntryPoint = "test", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] internal static extern Int32 test(); static void Main(string[] args) { test(); Console.WriteLine("SUCCESS"); } } } </pre><p> Some details: </p> <ul><li>We are using Visual Studio 2015 14 and dynamically link the c++ runtime. </li><li>The test library statically links Boost 1.63.0. </li><li>We also tried to reproduce this behaviour with calling the functionallity directly from c++ and from python. Both tests have not been successful so far. </li><li>If you start the c# code with CTRL F5 (meaning without the .net debugger) everything will also be fine. Only if you start it with F5 (meaning the .NET Debugger attached) the visual studio instance will crash. Also be sure not to enable the native debugger! </li><li>Note: If we don't use the exceptions in the stream, everything seams to be fine as well. Unfortunately the code decoding my objects makes use of them and therefore I cannot avoid this. </li></ul><p> Would be amazing if you had some additional hints on what might go wrong here or a solution. </p> <p> Thanks in advance! Best Regards, Michael </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13245 Trac 1.4.3 Michael Eiler <michael.eiler@…> Mon, 09 Oct 2017 17:24:14 GMT keywords, version, component changed https://svn.boost.org/trac10/ticket/13245#comment:1 https://svn.boost.org/trac10/ticket/13245#comment:1 <ul> <li><strong>keywords</strong> context added </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.63.0</span> → <span class="trac-field-new">Boost 1.65.0</span> </li> <li><strong>component</strong> <span class="trac-field-old">coroutine</span> → <span class="trac-field-new">context</span> </li> </ul> <p> Hi, </p> <p> I did some further investigations and can provide some more details. The problem also occurs in the newest boost release (1.65.1). The behavior is still the same. It only crashes when the C# debugger (without native debugging) is attached but I can directly reproduce it with context, the coroutine2 wrapper around it is not necessary. </p> <p> This is the updated content of the Api.cpp file: </p> <pre class="wiki">#include &lt;boost/coroutine2/coroutine.hpp&gt; #include &lt;boost/context/all.hpp&gt; class Foo { public: void bar() { throw std::exception("Hello World!"); } }; int ApiInit() { boost::context::continuation source = boost::context::callcc( [](boost::context::continuation &amp;&amp; sink) { Foo foo; try { foo.bar(); } catch (const std::exception&amp; ex) { std::cerr &lt;&lt; "caught ex: " &lt;&lt; ex.what() &lt;&lt; std::endl; } return std::move(sink); }); if (source.operator bool()) source = source.resume(); return 0; } </pre><p> It's important that this is compiled without optimizations enabled (e.g. in debug mode) so that throwing the exception won't be optimized away. Because the actual issue is exactly the process of throwing an exception. </p> <p> Best Regards, </p> <p> Michael </p> Ticket Michael Eiler <michael.eiler@…> Thu, 30 Nov 2017 06:38:12 GMT <link>https://svn.boost.org/trac10/ticket/13245#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13245#comment:2</guid> <description> <p> In a first quick test with boost 1.65.1 and windows fibers as implementation for boost-context I haven't been able to reproduce the issue anymore. My current suspision is that the .Net Debugger in some circumstances might use additional registers which are not backuped by the default fcontext implementation. </p> </description> <category>Ticket</category> </item> </channel> </rss>