Monday, May 21, 2012

pass a C++ object instance to python, but call C++ functions contain STL have exception in python

I am using SWIG to wrapper C++ dll. C++ call pyhon, but need to transfer a object instance to python. In python , it call some C++ function using this instance.

1 first, use:

 swig_type_info _swigt__p_CallFactory = 
{"_p_CallFactory", "CallFactory *", 0, 0, (void*)0, 0};

instance =
SWIG_NewPointerObj(SWIG_as_voidptr(callF), &(_swigt__p_CallFactory), 0);

pass a C++ object instance to python.

2 in python, it get SWIG_PyObject pointer. then I need to change it to CallFactory*.

in python: 
callFactory = example.Conver_to_callFactory (callF)

the Conver_to_callFactory fucntion is like this:

  CallFactory* c_ptr;

swig_type_info *ty = SWIG_TypeQuery("CallFactory *");

printf("CallFactory ty %p \n", ty);

if (SWIG_ConvertPtr(py_obj, (void **) &c_ptr, ty, 0) == -1)
c_ptr = 0;

3 now I get CallFactory* in python which is newed in C++

4 now I call CallFactory method in python:


in C++ , the CallFactory::CreateStaticCall() contains STL operation :
intVec.push_back(2); intVec in a data member of CallFactory: std::vector<int>.

when pragram run here, it crash. I can't get anything useful.

And when call other fucntions without STL opreation, it is ok.

Could you help me, please? Thank you

No comments:

Post a Comment