Hi all,<br><br>A month ago or so we discovered a bug in Boost.Python
that manifests when using enable_shared_from_this.&nbsp;&nbsp;Here is a test case
that demonstrates the bug:<br><br>
<div style="margin-left: 40px;">#include &lt;boost/python.hpp&gt;<br>
#include &lt;boost/enable_shared_from_this.hpp&gt;<br>
<br>
using namespace boost;<br>
using namespace boost::python;<br>
<br>
class Test;<br>
typedef shared_ptr&lt;Test&gt; TestPtr;<br>
<br>
class Test : public enable_shared_from_this&lt;Test&gt; {<br>
public:<br>
&nbsp;&nbsp;&nbsp;&nbsp;static TestPtr construct() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TestPtr(new Test);<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;void act() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestPtr kungFuDeathGrip(shared_from_this());<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;void take(TestPtr t) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
};<br>
<br>
void Export_test() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;class_&lt;Test, TestPtr, noncopyable&gt;(&quot;Test&quot;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.def(&quot;construct&quot;, &amp;Test::construct)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.staticmethod(&quot;construct&quot;)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.def(&quot;act&quot;, &amp;Test::act)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.def(&quot;take&quot;, &amp;Test::take)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;<br>
}<br>
</div>
<br>
And here is the Python:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = Test.construct()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.take(x)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x.act()<br>
<br>
The bug (as I understand it) is that the shared_ptr_from_python
converter creates a new shared_ptr for the Test instance to pass it
into C++.&nbsp; This new shared_ptr has a nop deleter, except that it keeps
the Python object alive as long as the new shared_ptr is alive.&nbsp; The
problem here is that creating a shared_ptr to an object of type T when
T is enable_shared_from_this&lt;T&gt; resets the weak pointer inside of
enable_shared_from_this&lt;T&gt;.&nbsp; (See shared_ptr&#39;s constructors for
the details of the implementation.)&nbsp; A fix that passes the test above
and has worked for us is to change shared_ptr&#39;s constructor to accept a
dont_enable_shared_from_this argument and pass that in from the
shared_ptr_from_python converter.&nbsp; The patch is attached (against boost
1.33, but the bug didn&#39;t look fixed in 1.34 either).<br>
<br>Cheers,<br>Chad<br><br>p.s. I have seen other people with this problem as well.&nbsp; Googling for bad_weak_ptr turns up several results.<br>