[capi-sig] Python C Api extension (multi threaded environment) Raises exception (Accesss violation error)

Jadhav, Alok alok.jadhav at credit-suisse.com
Tue Dec 8 01:09:14 CET 2009


I am using SWIG to extend Python to C++.   But the questions I have
consists of basic Python C API related. 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 data types
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. ... 

[code]
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); 
}
[/code]


Python function call..

[code]
def callback_fun(msg):
    try:
        print "RFAMessage received for service:"+msg.service
    except Exception:
        print "Exception handled"
[/code]

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 in Python. But only when I pass it
from C++ to python I get this exception.


I have spent long time to solve this issue but in vain. I hope I get
some help from this Forum.


Regards, 
Alok

=============================================================================== 
 Please access the attached hyperlink for an important electronic communications disclaimer: 
 http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html 
 =============================================================================== 
 


More information about the capi-sig mailing list