<div dir="ltr"><font size="4"><b>I abstract the problem below:</b></font><br><br>t1.cpp:<br><br>#include <boost/python.hpp><br>#include <boost/enable_shared_from_this.hpp><br>#include <boost/shared_ptr.hpp><br> #include <iostream><br>#include <string><br>#include <list><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<A><br> {<br>public:<br> void test();<br>};<br><br>class X<br>{<br>public:<br> void test(boost::shared_ptr<A> pObj){}<br>};<br><br>typedef boost::shared_ptr < A > A_ptr;<br>typedef boost::shared_ptr < X > X_ptr;<br> <br>X_ptr xx;<br><br>void A::test()<br>{<br> object aa = global[ "aa" ];<br> A_ptr pa( shared_from_this() );<br> aa( xx, pa );<br>}<br><br><br>BOOST_PYTHON_MODULE(pythonobject)<br>{<br> class_ <A>("A")<br> .def("test", &A::test)<br> ;<br> <br> class_ <X>("X")<br> .def("test", &X::test)<br> ;<br><br> register_ptr_to_python <A_ptr>();<br> register_ptr_to_python <X_ptr>();<br> }<br><br>int main()<br>{<br> Py_Initialize();<br><br> initpythonobject();<br> PyRun_SimpleString("import pythonobject");<br> mainobj = import("__main__");<br> global=(mainobj.attr("__dict__"));<br> <br> A_ptr a( new A );<br> X_ptr x( new X );<br> xx = x;<br><br> try {<br> exec_file("t1.py", global, global);<br> <br> a->test();<br> a->test();<br> a->test();<br> } catch (error_already_set) {<br> PyErr_Print();<br> }<br><br> return 0;<br>}<br><br>t1.py:<br><br>#!/usr/bin/python<br>#coding:utf-8<br>def aa(x,a):<br> x.test( a )<br> 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 'boost::bad_weak_ptr'<br> what(): tr1::bad_weak_ptr<br><br><br>When debug the program, I found after first "a->test()" call,<br>a._internal_weak_this.use_count became 0. It's obviously wrong...<br> so the after "a->test()" will cause error.<br><br><br>But if change <br> void test(boost::shared_ptr<A> pObj){}<br>to<br> void 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>