Multiple Initialize, RunSimpleString, Shutdown calls result in AccessViolationException

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Multiple Initialize, RunSimpleString, Shutdown calls result in AccessViolationException

Oleksii Bidiuk
Hi All,

I am seeing weird problems with embedding PythonNET/Python 2.6 in a C#, using 4.0 runtime application built with VS2010 on Windows XP SP3. 

Design outline
--------------------
For every script I execute the following sequence on a separate thread (does not seem to matter the same or different one every time) to avoid blocking GUI:

PythonEngine.Initialize();

<the following block executed several times with startup scripts and then the actual user script>
IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString(source);
PythonEngine.ReleaseLock(pythonLock);

PythonEngine.Shutdown();

I use the RunSimpleString() as the RunString() didn't work for me (always returns NULL and does not execute the code, suggestions welcome!).

Problem description
-----------------------------
Running simple scripts like 'print "whatever"' goes well. I can run scripts multiple (>10) times without any side-effects. Problems start when importing certain modules (like ctypes, numpy), or accessing collections of .NET objects returned by the .NET API. For example, the following scripts will fail executing SECOND time. Whenever I call the script AGAIN I get AccessViolationException or even ExecutionEngineException (with debugger attached of course) in the Shutdown() call. 

Example 1
---------------
import ctypes

Exampe 2
--------------
import clr
clr.AddReference("System.Drawing")
import System.Drawing
bmp = System.Drawing.Bitmap("c:\\image.bmp")

I should be doing something completely wrong, but I just cannot see it... 

Please help!

--
oleksii

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet