Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f56ebc

Browse files
amos402filmor
authored andcommitted
Fix refcnt errors (split from #958) (#1001)
* Add exception helper * Fixed refcnt error in ExtensionType.FinalizeObject * Fixed typename leaking * Fix refcnt error by using `using`
1 parent d1044c3 commit 5f56ebc

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dotnet_sort_system_directives_first = true
2525
dotnet_separate_import_directive_groups = true
2626

2727
[*.cs]
28-
csharp_new_line_before_open_brace = true
28+
csharp_new_line_before_open_brace = all
2929
csharp_new_line_before_else = true
3030
csharp_new_line_before_catch = true
3131
csharp_new_line_before_finally = true

src/runtime/extensiontype.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public ExtensionType()
3838

3939
Runtime.PyObject_GC_UnTrack(py);
4040

41+
// Steals a ref to tpHandle.
4142
tpHandle = tp;
4243
pyHandle = py;
4344
gcHandle = gc;
@@ -50,7 +51,7 @@ public ExtensionType()
5051
public static void FinalizeObject(ManagedType self)
5152
{
5253
Runtime.PyObject_GC_Del(self.pyHandle);
53-
Runtime.XDecref(self.tpHandle);
54+
// Not necessary for decref of `tpHandle`.
5455
self.gcHandle.Free();
5556
}
5657

src/runtime/metatype.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ private static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
266266
return Runtime.PyFalse;
267267
}
268268

269+
Runtime.XIncref(args);
269270
using (var argsObj = new PyList(args))
270271
{
271272
if (argsObj.Length() != 1)

src/runtime/pythonexception.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23

34
namespace Python.Runtime
45
{
@@ -190,5 +191,21 @@ public static bool Matches(IntPtr ob)
190191
{
191192
return Runtime.PyErr_ExceptionMatches(ob) != 0;
192193
}
194+
195+
public static void ThrowIfIsNull(IntPtr ob)
196+
{
197+
if (ob == IntPtr.Zero)
198+
{
199+
throw new PythonException();
200+
}
201+
}
202+
203+
public static void ThrowIfIsNotZero(int value)
204+
{
205+
if (value != 0)
206+
{
207+
throw new PythonException();
208+
}
209+
}
193210
}
194211
}

src/runtime/runtime.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,10 @@ internal static IntPtr PyUnicode_FromStringAndSize(IntPtr value, long size)
13681368

13691369
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
13701370
private static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, IntPtr size);
1371+
1372+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1373+
internal static extern IntPtr PyUnicode_AsUTF8(IntPtr unicode);
1374+
13711375
#elif PYTHON2
13721376
internal static IntPtr PyString_FromStringAndSize(string value, long size)
13731377
{

src/runtime/typemanager.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,8 @@ internal static IntPtr AllocateTypeObject(string name)
409409
// the Python version of the type name - otherwise we'd have to
410410
// allocate the tp_name and would have no way to free it.
411411
#if PYTHON3
412-
// For python3 we leak two objects. One for the ASCII representation
413-
// required for tp_name, and another for the Unicode representation
414-
// for ht_name.
415-
IntPtr temp = Runtime.PyBytes_FromString(name);
416-
IntPtr raw = Runtime.PyBytes_AS_STRING(temp);
417-
temp = Runtime.PyUnicode_FromString(name);
412+
IntPtr temp = Runtime.PyUnicode_FromString(name);
413+
IntPtr raw = Runtime.PyUnicode_AsUTF8(temp);
418414
#elif PYTHON2
419415
IntPtr temp = Runtime.PyString_FromString(name);
420416
IntPtr raw = Runtime.PyString_AsString(temp);

0 commit comments

Comments
 (0)