[Python.NET] Multiple Initialize, RunSimpleString, Shutdown calls result in AccessViolationException

Oleksii Bidiuk oleksii.bidiuk at gmail.com
Wed Feb 9 12:03:12 CET 2011

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


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


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
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!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20110209/38bfa518/attachment.html>

More information about the PythonDotNet mailing list