Boost C++ Libraries: Ticket #1952: [saywhat] The operation static_cast<std::string>(boost::python::extract<std::string>(...)) crashes Python. Seems to create reference counting problem. https://svn.boost.org/trac10/ticket/1952 <p> I submit boost::python based python extension code that crashes Python interpreter when used as described below. I attach Visual Studio .NET project files. </p> <p> C:\KDocuments\Variance Swap Validation 2\VarSw2\Debug&gt;python <a class="missing wiki">ActivePython</a> 2.5.2.2 (<a class="missing wiki">ActiveState</a> Software Inc.) based on Python 2.5.2 (<a class="source" href="https://svn.boost.org/trac10/log/?revs=252-60911">r252:60911</a>, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. </p> <blockquote class="citation"> <blockquote class="citation"> <blockquote class="citation"> <p> import vrsw2 p=vrsw2.<a class="missing wiki">VarSwapTest</a>() p.set(((1,2),(3,4))) </p> </blockquote> </blockquote> </blockquote> <p> &lt;crash here&gt; </p> <p> The C++ extension code: #include &lt;string&gt; #include &lt;fstream&gt; #include &lt;boost/python.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; using namespace boost::python; </p> <p> class <a class="missing wiki">VarSwapTest</a> { private: </p> <blockquote> <p> class Impl { private: </p> <blockquote> <p> std::ofstream theFile; </p> </blockquote> <p> public: </p> <blockquote> <p> Impl() </p> <blockquote> <p> : theFile("<a class="missing wiki">VarSwapTestEcho</a>.txt") {} </p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <p> void set( object&amp; input ) { </p> <blockquote> <p> object o=input.attr("<span class="underline">str</span>")(); extract&lt;std::string&gt; ex(o); if( ex.check() ) </p> <blockquote> <p> theFile&lt;&lt;(static_cast&lt;std::string&gt;(ex()))&lt;&lt;std::endl; </p> </blockquote> <p> else </p> <blockquote> <p> theFile&lt;&lt;"failed to extract string"&lt;&lt;std::endl; </p> </blockquote> </blockquote> <p> } </p> </blockquote> <p> }; </p> </blockquote> <p> private: </p> <blockquote> <p> boost::shared_ptr&lt;Impl&gt; theImpl; </p> </blockquote> <p> public: </p> <blockquote> <p> <a class="missing wiki">VarSwapTest</a>() : theImpl(new Impl()) {} void set( object input ) { </p> <blockquote> <p> theImpl-&gt;set(input); </p> </blockquote> <p> } </p> </blockquote> <p> }; </p> <p> BOOST_PYTHON_MODULE(vrsw2) { </p> <blockquote> <p> class_&lt;<a class="missing wiki">VarSwapTest</a>&gt;("<a class="missing wiki">VarSwapTest</a>") </p> <blockquote> <p> .def("set",&amp;VarSwapTest::set) </p> </blockquote> <p> ; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1952 Trac 1.4.3 Konstantin Aslanidi <konstantin.aslanidi@…> Tue, 27 May 2008 14:50:04 GMT attachment set https://svn.boost.org/trac10/ticket/1952 https://svn.boost.org/trac10/ticket/1952 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">VarSw2.zip</span> </li> </ul> Ticket Konstantin Aslanidi <konstantin.aslanidi@…> Tue, 27 May 2008 15:02:57 GMT <link>https://svn.boost.org/trac10/ticket/1952#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1952#comment:1</guid> <description> <p> Less complicated inputs do not create immediate problem: p.set((1,2)) p.set('d') </p> <p> are fine. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Tue, 27 May 2008 18:37:18 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/1952#comment:2 https://svn.boost.org/trac10/ticket/1952#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> suppose you replace </p> <pre class="wiki">static_cast&lt;std::string&gt;(boost::python::extract&lt;std::string&gt;(...)) </pre><p> with </p> <pre class="wiki">boost::python::extract&lt;std::string&gt;(...)() // note extra parens </pre><p> If the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor. </p> Ticket Konstantin Aslanidi <konstantin.aslanidi@…> Tue, 27 May 2008 19:42:04 GMT <link>https://svn.boost.org/trac10/ticket/1952#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1952#comment:3</guid> <description> <p> Dave, </p> <p> I tried all combinations, I do use extra perens in the submitted code. The code compiles, links, runs. It succeds for list,dicts,strings etc. It fails for embedded structures such as tuple of tuples. </p> <p> For example: import vrsw2 p=vrsw2.<a class="missing wiki">VarSwapTest</a>() p.set((1,2)) p.set('d') </p> <p> are successful. </p> <p> However, p.set(((1,2),(3,4))) crashes. I did some debugging, it appears that the crash occurs during heap deallocation. I was unable to find any version of the submitted code that would pass the tuple of tuples. </p> <p> K. </p> </description> <category>Ticket</category> </item> <item> <author>Konstantin Aslanidi <konstantin.aslanidi@…></author> <pubDate>Tue, 27 May 2008 19:43:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1952#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1952#comment:4</guid> <description> <p> Please, see the newly submitted correction. </p> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/1952#comment:2" title="Comment 2">dave</a>: </p> <blockquote class="citation"> <p> suppose you replace </p> <pre class="wiki">static_cast&lt;std::string&gt;(boost::python::extract&lt;std::string&gt;(...)) </pre><p> with </p> <pre class="wiki">boost::python::extract&lt;std::string&gt;(...)() // note extra parens </pre><p> If the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor. </p> </blockquote> </description> <category>Ticket</category> </item> <item> <author>Konstantin Aslanidi <konstantin.aslanidi@…></author> <pubDate>Tue, 27 May 2008 20:28:09 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1952#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1952#comment:5</guid> <description> <p> ... in addition to previous comments, I verified the below suggestion. It does not work. </p> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/1952#comment:2" title="Comment 2">dave</a>: </p> <blockquote class="citation"> <p> suppose you replace </p> <pre class="wiki">static_cast&lt;std::string&gt;(boost::python::extract&lt;std::string&gt;(...)) </pre><p> with </p> <pre class="wiki">boost::python::extract&lt;std::string&gt;(...)() // note extra parens </pre><p> If the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor. </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>troy d. straszheim</dc:creator> <pubDate>Thu, 22 Oct 2009 04:39:19 GMT</pubDate> <title>status, summary changed; cc, resolution set https://svn.boost.org/trac10/ticket/1952#comment:6 https://svn.boost.org/trac10/ticket/1952#comment:6 <ul> <li><strong>cc</strong> <span class="trac-author">troy@…</span> added </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> <li><strong>summary</strong> <span class="trac-field-old">The operation static_cast&lt;std::string&gt;(boost::python::extract&lt;std::string&gt;(...)) crashes Python. Seems to create reference counting problem.</span> → <span class="trac-field-new">[saywhat] The operation static_cast&lt;std::string&gt;(boost::python::extract&lt;std::string&gt;(...)) crashes Python. Seems to create reference counting problem.</span> </li> </ul> <p> unable to reproduce with recent gcc. Test case simplified and added to my branch in git, will test on vc9, if it doesn't pass there i'll reopen. </p> <p> the test: </p> <p> <a class="ext-link" href="http://gitorious.org/~straszheim/boost/straszheim/commit/43829f0e3290b74aa88907a25698e30c573b1012"><span class="icon">​</span>http://gitorious.org/~straszheim/boost/straszheim/commit/43829f0e3290b74aa88907a25698e30c573b1012</a> </p> Ticket