<div dir="ltr"><font size="4"><b>I abstract the problem below:</b></font><br><br>t1.cpp:<br><br>#include &lt;boost/python.hpp&gt;<br>#include &lt;boost/enable_shared_from_this.hpp&gt;<br>#include &lt;boost/shared_ptr.hpp&gt;<br>
#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>#include &lt;list&gt;<br>using namespace std;<br>using namespace boost::python;<br><br>static object mainobj, global;<br><br>class A : public boost::enable_shared_from_this&lt;A&gt;<br>
{<br>public:<br>&nbsp;&nbsp;&nbsp; void test();<br>};<br><br>class X<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; void&nbsp;&nbsp;&nbsp; test(boost::shared_ptr&lt;A&gt; pObj){}<br>};<br><br>typedef boost::shared_ptr &lt; A &gt; A_ptr;<br>typedef boost::shared_ptr &lt; X &gt; X_ptr;<br>
<br>X_ptr xx;<br><br>void A::test()<br>{<br>&nbsp;&nbsp;&nbsp; object aa = global[ &quot;aa&quot; ];<br>&nbsp;&nbsp;&nbsp; A_ptr pa( shared_from_this() );<br>&nbsp;&nbsp;&nbsp; aa( xx, pa );<br>}<br><br><br>BOOST_PYTHON_MODULE(pythonobject)<br>{<br>&nbsp;&nbsp;&nbsp; class_ &lt;A&gt;(&quot;A&quot;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .def(&quot;test&quot;, &amp;A::test)<br>&nbsp;&nbsp;&nbsp; ;<br>&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; class_ &lt;X&gt;(&quot;X&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .def(&quot;test&quot;, &amp;X::test)<br>&nbsp;&nbsp;&nbsp; ;<br><br>&nbsp;&nbsp;&nbsp; register_ptr_to_python &lt;A_ptr&gt;();<br>&nbsp;&nbsp;&nbsp; register_ptr_to_python &lt;X_ptr&gt;();<br>
}<br><br>int main()<br>{<br>&nbsp;&nbsp;&nbsp; Py_Initialize();<br><br>&nbsp;&nbsp;&nbsp; initpythonobject();<br>&nbsp;&nbsp;&nbsp; PyRun_SimpleString(&quot;import pythonobject&quot;);<br>&nbsp;&nbsp;&nbsp; mainobj = import(&quot;__main__&quot;);<br>&nbsp;&nbsp;&nbsp; global=(mainobj.attr(&quot;__dict__&quot;));<br>
&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; A_ptr a( new A );<br>&nbsp;&nbsp;&nbsp; X_ptr x( new X );<br>&nbsp;&nbsp;&nbsp; xx = x;<br><br>&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exec_file(&quot;t1.py&quot;, global, global);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a-&gt;test();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a-&gt;test();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a-&gt;test();<br>
&nbsp;&nbsp;&nbsp; } catch (error_already_set) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyErr_Print();<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; return 0;<br>}<br><br>t1.py:<br><br>#!/usr/bin/python<br>#coding:utf-8<br>def aa(x,a):<br>&nbsp;&nbsp;&nbsp; x.test( a )<br>&nbsp;&nbsp;&nbsp; return 1<br><br><br><font size="4"><b>after execute t1.cpp , I got this erro info:<br>
<br>terminate called after throwing an instance of &#39;boost::bad_weak_ptr&#39;<br>&nbsp; what():&nbsp; tr1::bad_weak_ptr<br><br><br>When debug the program, I found after first &quot;a-&gt;test()&quot; call,<br>a._internal_weak_this.use_count became 0. It&#39;s obviously wrong...<br>
so the after &quot;a-&gt;test()&quot; will cause error.<br><br><br>But if change <br>&nbsp;&nbsp;&nbsp; void&nbsp;&nbsp;&nbsp; test(boost::shared_ptr&lt;A&gt; pObj){}<br>to<br>&nbsp;&nbsp;&nbsp; void&nbsp;&nbsp;&nbsp; test(A* pObj){}<br><br>I got no error. The errror is gone.<br>Will someone explain this prolbem to me? Thank you.</b></font><br>
<br></div>