I am using SWIG to extend Python to C++. Understand that there is a different forum for SWIG users but I am still waiting for a response on that list. But the questions I have consists of basic Python C API releated. I hope this list will be able to help me.
I am trying to solve a simple problem which many of you must have encountered. I wonder what I am doing different than others that I am facing a problem.
Background
-----------
A c++ library is wrapped in my C++ Class. This new C++ class is extended in Python using SWIG. I have
to call a python function from C++ code ( which is on a new thread spawned from C++ main thread).
Issue
-----
I am able to call the function. No issues. I could pass basic datatypes such as int. No issues. But when I try to pass a C++ class object (eg RFAMessageWrapper) to my python function I get an exception. It is an Access violation exception. The memory could not be "read". I am not even able to catch this exception.
Some code snippets
--------------------
C++ function which calls python function. ...
static void PythonCallBack(RFAMessage *msg, void *clientdata)
{
PyObject *func, *arglist;
int blah = 1;
PyGILState_STATE state;
state = PyGILState_Ensure();
func = (PyObject *) clientdata; // Get Python function
RFAMessageWrapper msg2(msg);
try {
arglist = Py_BuildValue("(O)",msg2); // Build argument list
//arglist = Py_BuildValue("(i)", blah);
PyEval_CallObject(func,arglist); // Call Python
} catch (...) {
cout<<"Unknown exception..."<<endl;
}
Py_XDECREF(arglist); // Trash arglist
PyGILState_Release(state);
}
Python function call..
def callback_fun(msg):
try:
print "RFAMessage received for service:"+msg.service
except Exception:
print "Exception handled"
Whenever I try to access msg (of type RFAMessageWrapper .. C++ object) I get exception. msg is not None. I checked that in if condition. even type(msg) raises exception.
RFAMessageWrapper class has been wrapped properly by SWIG as I could create an object manually and use it well in Python. But only when I pass it from C++ to python I get this error.
I have spent long time to solve this issue but in vain. I hope I get some help from this Forum.
Regards,
Alok