<!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>&nbsp;<FONT SIZE=2 FACE="Arial"></FONT>&nbsp;<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 &quot;read&quot;.&nbsp; 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">&nbsp;&nbsp; PyObject *func, *arglist;</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; int blah = 1;</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp; </FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; PyGILState_STATE state; </FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; state = PyGILState_Ensure();</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; func = (PyObject *) clientdata;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Get Python function</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; RFAMessageWrapper msg2(msg);</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">try {</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">arglist = Py_BuildValue(&quot;(O)&quot;,msg2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Build argument list</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">//arglist = Py_BuildValue(&quot;(i)&quot;, blah);</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">PyEval_CallObject(func,arglist);&nbsp;&nbsp;&nbsp;&nbsp; // Call Python</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">} catch (...) {</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">cout&lt;&lt;&quot;Unknown exception...&quot;&lt;&lt;endl;</FONT>

<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">}</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp; Py_XDECREF(arglist);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Trash arglist</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; 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">&nbsp;&nbsp;&nbsp; try:</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;RFAMessage received for service:&quot;+msg.service</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; except Exception:</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Exception handled&quot;</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>