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

Ulf Worsoe ulf.worsoe at mosek.com
Tue Dec 8 09:29:47 CET 2009


Hi,

I don't have any experience with SWIG, but I can see two places where
things may go wrong:
1) If the call back function was garbage collected before you called
it (probably not the case if it is a normal function on the Python
side)
2) I assume that RFAMessageWrapper is an extension of a PyObject that
swig generated; in this case you should probably build arglist using a
pointer to, ie:
  arglist = Py_BuildValue("(O)",&msg2);


--
Ulf Worsøe
Mosek ApS


On Tue, Dec 8, 2009 at 1:09 AM, Jadhav, Alok
<alok.jadhav at credit-suisse.com> wrote:
> 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
>  ===============================================================================
>
> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig
>


More information about the capi-sig mailing list