[C++-sig] Python C++ extension (multi threaded) raises access violation exception

Gustavo Carneiro gjcarneiro at gmail.com
Tue Dec 8 01:18:40 CET 2009


2009/12/8 Jadhav, Alok <alok.jadhav at credit-suisse.com>

>  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
>
This is bad.   Py_BuildValue("(O)", is expecting to find a PyObject, but msg2
is not a PyObject.

Additionally, PyObjects must be created with PyObject_New, and never stack
allocated like msg2 is.

I cannot offer any solution, as it would be SWIG specific and I am not
familiar with SWIG.


>                 //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
>
> ==============================================================================
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20091208/27f3f341/attachment-0001.htm>


More information about the Cplusplus-sig mailing list