[Python.NET] Several Python.Runtime problems
Barton
barton at BCDesignsWell.com
Thu Jan 26 09:11:14 CET 2012
Thanks, Alexey,
I've applied your patches to the trunk.
It's great to have on the project!
On 1/23/2012 12:33 PM, Alexey Borzenkov wrote:
> 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.
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
>
More information about the PythonDotNet
mailing list