Boost C++ Libraries: Ticket #9525: Assert with program termination due to context<...>() call in simple_state's entry action is incorrect https://svn.boost.org/trac10/ticket/9525 <pre class="wiki">#include &lt;boost/statechart/state_machine.hpp&gt; #include &lt;boost/statechart/simple_state.hpp&gt; namespace sc = boost::statechart; struct state; struct fsm : sc::state_machine&lt;fsm, state&gt; { void test() {} }; struct state : sc::simple_state&lt;state, fsm&gt; { state() { context&lt;fsm&gt;().test(); } }; int main() { fsm test; test.initiate(); } </pre><pre class="wiki">statechart_test: /usr/include/boost/statechart/simple_state.hpp:682: static OtherContext&amp; boost::statechart::simple_state&lt;MostDerived, Context, InnerInitial, historyMode&gt;::context_impl_other_context::context_impl(State&amp;) [with OtherContext = fsm; State = boost::statechart::simple_state&lt;state, fsm&gt;; MostDerived = state; Context = fsm; InnerInitial = boost::mpl::list&lt;mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na&gt;; boost::statechart::history_mode historyMode = (boost::statechart::history_mode)0u]: Assertion `get_pointer( stt.pContext_ ) != 0' failed. Аварийный останов </pre><p> It's not correct to throw uncatchable surprises like asserts. It's especially incorrect when your library is used in the embedded software. </p> <p> Not to mention this isn't reflected anywhere in the documentation. </p> <p> Please change this to exception throwing, or better, find a way to detect calls like that at compile time. But don't use asserts. That's just bad treatment of your users. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9525 Trac 1.4.3 Andreas Huber Tue, 31 Dec 2013 14:25:20 GMT <link>https://svn.boost.org/trac10/ticket/9525#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9525#comment:1</guid> <description> <p> Replying to <a class="new ticket" href="https://svn.boost.org/trac10/ticket/9525" title="#9525: Bugs: Assert with program termination due to context&lt;...&gt;() call in ... (new)">anonymous</a>: </p> <blockquote class="citation"> <p> It's not correct to throw uncatchable surprises like asserts. It's especially incorrect when your library is used in the embedded software. </p> <p> Not to mention this isn't reflected anywhere in the documentation. </p> </blockquote> <p> Do you use std::vector? If yes, I'm wondering what you get when you dereference the return value of std::vector::end()? Last time I accidentally did that I got an assertion in DEBUG mode. I RELEASE mode, on most platforms dereferencing the return value of std::vector::end() does the wrong thing and return the contents of an invalid memory location. IIRC, the standard just says that the iterator you get from vector::end() must not be dereferenced. </p> <p> I would argue that it's an almost identical situation with the simple_state::context() function. The reference docs say that you can't call this function in the constructor of a simple_state subclass: </p> <p> &lt;<a href="http://www.boost.org/doc/libs/1_55_0/libs/statechart/doc/reference.html#ClassTemplatesimple_state">http://www.boost.org/doc/libs/1_55_0/libs/statechart/doc/reference.html#ClassTemplatesimple_state</a>&gt; </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 20 Jan 2014 13:18:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9525#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9525#comment:2</guid> <description> <p> It says "must not call", but it doesn't say *anything* about getting an assert and the program termination when you make a mistake (e.g. add context() call to a simple_state constructor but forget to change simple_state into state). Is this really the only way you can say "you've made a mistake" here? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Sun, 26 Jan 2014 18:12:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9525#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9525#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9525#comment:2" title="Comment 2">anonymous</a>: </p> <blockquote class="citation"> <p> It says "must not call", but it doesn't say *anything* about getting an assert and the program termination when you make a mistake (e.g. add context() call to a simple_state constructor but forget to change simple_state into state). </p> </blockquote> <p> IIRC, neither do the standard docs for std::vector::end. </p> <blockquote class="citation"> <p> Is this really the only way you can say "you've made a mistake" here? </p> </blockquote> <p> The only other thing that comes to mind is throwing an exception, but exceptions have the problem that they can be ignored. Calling context() from a simple_state constructor makes the program broken beyond any hope of recovery. If you have any other suggestions, I'm all ears. </p> </description> <category>Ticket</category> </item> </channel> </rss>