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