Boost C++ Libraries: Ticket #13006: serialization optional doesn't initialize stack_construct causes crashes https://svn.boost.org/trac10/ticket/13006 <p> Boost\serialization\optional.hpp line 94 </p> <p> detail::stack_allocate&lt;T&gt; tp; </p> <p> after some analysis we found that it should have been: detail::stack_construct&lt;Archive, T&gt; tp(ar, item_version); </p> <p> which then solves the crash </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13006 Trac 1.4.3 Robert Ramey Wed, 03 May 2017 20:48:41 GMT <link>https://svn.boost.org/trac10/ticket/13006#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13006#comment:1</guid> <description> <p> hmmmm - this looks like the implementation was changed in oct 11 2013 to the current one to one using stack_allocate. Of course now I don't remember the motivation for this change. I'm pretty skeptical that changing it back would result in a net gain. I'm guessing that the correct fix is more likely something like: </p> <pre class="wiki">if (version &lt; ?){ detail::stack_construct&lt;Archive, T&gt; aux(ar, item_version); ar &gt;&gt; boost::serialization::make_nvp("value", aux.reference()); t.reset(aux.reference()); } else{ detail::stack_allocate&lt;T&gt; tp; ar &gt;&gt; boost::serialization::make_nvp("value", tp.reference()); t.reset(boost::move(tp.reference())); ar.reset_object_address( t.get_ptr(), &amp; tp.reference() ); </pre><p> Would that have fixed your issue? - I would be interested in your thoughts on this. </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Wed, 03 May 2017 21:41:11 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13006#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13006#comment:2</guid> <description> <p> that's not it. Need to look at this some more. </p> </description> <category>Ticket</category> </item> <item> <author>alexanderponkratov@…</author> <pubDate>Fri, 05 May 2017 14:51:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13006#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13006#comment:3</guid> <description> <p> read access violation while loading boost::optional&lt;std::map&lt;std::string, boost::shared_ptr&lt;MyUDT&gt; &gt; &gt; </p> <p> boost 1.64, compiler MSVC-2017. This code works fine with boost 1.63, but fails with 1.64. </p> <pre class="wiki">#include &lt;fstream&gt; #include &lt;map&gt; #include &lt;string&gt; #include &lt;boost/optional.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/archive/binary_oarchive.hpp&gt; #include &lt;boost/archive/binary_iarchive.hpp&gt; #include &lt;boost/serialization/map.hpp&gt; #include &lt;boost/serialization/optional.hpp&gt; #include &lt;boost/serialization/shared_ptr.hpp&gt; #include &lt;boost/serialization/string.hpp&gt; struct MyUDT { boost::optional&lt;std::map&lt;std::string, boost::shared_ptr&lt;MyUDT&gt; &gt; &gt; val; template&lt;class Archive&gt; void serialize(Archive &amp; ar, const unsigned int version) { ar &amp; val; } }; int main() { std::ofstream ofs("filename"); MyUDT obj; obj.val = std::map&lt;std::string, boost::shared_ptr&lt;MyUDT&gt; &gt;(); obj.val.get()["test_key"] = boost::shared_ptr&lt;MyUDT&gt;(new MyUDT()); // save data to archive { boost::archive::binary_oarchive oa(ofs); // write class instance to archive oa &lt;&lt; obj; // archive and stream closed when destructors are called } // ... some time later restore the class instance to its orginal state MyUDT newObj; { // create and open an archive for input std::ifstream ifs("filename"); boost::archive::binary_iarchive ia(ifs); // read class state from archive ia &gt;&gt; newObj; // ERROR: read access violation // archive and stream closed when destructors are called } return 0; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Mon, 08 May 2017 16:24:08 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/13006#comment:4 https://svn.boost.org/trac10/ticket/13006#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Simplified implementation of serialization of boost::optional and believe it will be faster and more correct. I did test the example and enhanced the test for serialization of boost optional. I was going to implement serialization for std::optional but found that it's not out until C++17 and we don't have the config macros ready for that yet. It's checked into the develop branch. I just have to remember to merge to master before the next release. </p> Ticket