<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7654.12">
<TITLE>Python C++ extension (multi threaded) raises access violation exception</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P><FONT SIZE=2 FACE="Arial">I am using SWIG to extend Python to C++.</FONT> <FONT SIZE=2 FACE="Arial"></FONT> <FONT SIZE=2 FACE="Arial"> But the questions I have consists of basic Python C API related. I hope this list will be able to help me. </FONT></P>
<P><FONT SIZE=2 FACE="Arial">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.</FONT></P>
<P><FONT SIZE=2 FACE="Arial">Background</FONT>
<BR><FONT SIZE=2 FACE="Arial">-----------</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">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).</FONT></P>
<P><FONT SIZE=2 FACE="Arial">Issue</FONT>
<BR><FONT SIZE=2 FACE="Arial">-----</FONT>
<BR><FONT SIZE=2 FACE="Arial">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.</FONT></P>
<BR>
<BR>
<P><FONT SIZE=2 FACE="Arial">Some code snippets</FONT>
<BR><FONT SIZE=2 FACE="Arial">--------------------</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">C++ function which calls python function. ... </FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">[code]</FONT>
<BR><FONT SIZE=2 FACE="Arial">static void PythonCallBack(RFAMessage *msg, void *clientdata)</FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR><FONT SIZE=2 FACE="Arial"> PyObject *func, *arglist;</FONT>
<BR><FONT SIZE=2 FACE="Arial"> int blah = 1;</FONT>
<BR><FONT SIZE=2 FACE="Arial"> </FONT>
<BR><FONT SIZE=2 FACE="Arial"> PyGILState_STATE state; </FONT>
<BR><FONT SIZE=2 FACE="Arial"> state = PyGILState_Ensure();</FONT>
<BR><FONT SIZE=2 FACE="Arial"> func = (PyObject *) clientdata; // Get Python function</FONT>
<BR><FONT SIZE=2 FACE="Arial"> RFAMessageWrapper msg2(msg);</FONT>
<BR> <FONT SIZE=2 FACE="Arial">try {</FONT>
<BR> <FONT SIZE=2 FACE="Arial">arglist = Py_BuildValue("(O)",msg2); // Build argument list</FONT>
<BR> <FONT SIZE=2 FACE="Arial">//arglist = Py_BuildValue("(i)", blah);</FONT>
<BR> <FONT SIZE=2 FACE="Arial">PyEval_CallObject(func,arglist); // Call Python</FONT>
<BR> <FONT SIZE=2 FACE="Arial">} catch (...) {</FONT>
<BR> <FONT SIZE=2 FACE="Arial">cout<<"Unknown exception..."<<endl;</FONT>
<BR> <FONT SIZE=2 FACE="Arial">}</FONT>
<BR><FONT SIZE=2 FACE="Arial"> Py_XDECREF(arglist); // Trash arglist</FONT>
<BR><FONT SIZE=2 FACE="Arial"> PyGILState_Release(state); </FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
<BR><FONT SIZE=2 FACE="Arial">[/code]</FONT>
</P>
<BR>
<P><FONT SIZE=2 FACE="Arial">Python function call..</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">[code]</FONT>
<BR><FONT SIZE=2 FACE="Arial">def callback_fun(msg):</FONT>
<BR><FONT SIZE=2 FACE="Arial"> try:</FONT>
<BR><FONT SIZE=2 FACE="Arial"> print "RFAMessage received for service:"+msg.service</FONT>
<BR><FONT SIZE=2 FACE="Arial"> except Exception:</FONT>
<BR><FONT SIZE=2 FACE="Arial"> print "Exception handled"</FONT>
<BR><FONT SIZE=2 FACE="Arial">[/code]</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">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.</FONT></P>
<P><FONT SIZE=2 FACE="Arial">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</FONT> <FONT SIZE=2 FACE="Arial">exception</FONT><FONT SIZE=2 FACE="Arial">.</FONT></P>
<BR>
<P><FONT SIZE=2 FACE="Arial">I have spent long time to solve this issue but in vain. I hope I get some help from this Forum.</FONT>
</P>
<BR>
<P><FONT SIZE=2 FACE="Arial">Regards, </FONT>
<BR><FONT SIZE=2 FACE="Arial">Alok<SPAN LANG="ja"></SPAN></FONT>
</P>
</BODY>
</HTML><BR/><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><p class=MsoNormal><span lang=EN-US style='font-size:8.0pt;font-family:Courier'>==============================================================================<br>Please access the attached hyperlink for an important electronic communications disclaimer:<br><a href="http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html">http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html</a><br>==============================================================================<o:p></o:p></span></p></html><br>