[Python.NET] Several Python.Runtime problems
Alexey Borzenkov
snaury at gmail.com
Mon Jan 23 21:33:32 CET 2012
Hello,
While using Python.NET in one of my projects I found some problems:
1. PythonException uses Runtime.PyErr_Fetch before calling
AcquireLock, which is wrong. Here's the patch:
--- src/runtime/pythonexception.cs (revision 138)
+++ src/runtime/pythonexception.cs (working copy)
@@ -27,11 +27,11 @@
public PythonException() : base()
{
+ IntPtr gs = PythonEngine.AcquireLock();
Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
Runtime.Incref(_pyType);
Runtime.Incref(_pyValue);
Runtime.Incref(_pyTB);
- IntPtr gs = PythonEngine.AcquireLock();
if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero))
{
string type = new PyObject(_pyType).GetAttr("__name__").ToString();
2. Every time .NET code throws an exception Python.NET leaks memory,
because exception object is not decref'ed after calling
PyErr_SetObject. In my case there were lots of exceptions, so problem
was very noticable over time. Here's the patch:
-- src/runtime/exceptions.cs (revision 138)
+++ src/runtime/exceptions.cs (working copy)
@@ -473,6 +473,7 @@
IntPtr etype = Runtime.PyObject_GetAttrString(op, "__class__");
Runtime.PyErr_SetObject(etype, op);
Runtime.Decref(etype);
+ Runtime.Decref(op);
}
/// <summary>
Hope this helps others in their fights for thread safety and against
memory leaks. :)
Thanks,
Alexey.
More information about the PythonDotNet
mailing list