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

Skip to content

Commit 5e041af

Browse files
committed
on runtime shutdown from Python release all slot holders, not only the ones, that belong to managed types
1 parent eec30c0 commit 5e041af

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/runtime/TypeManager.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,21 @@ internal static void Initialize()
5151

5252
internal static void RemoveTypes()
5353
{
54-
foreach (var type in cache.Values)
54+
if (Runtime.HostedInPython)
5555
{
56-
if (Runtime.HostedInPython
57-
&& _slotsHolders.TryGetValue(type, out var holder))
56+
foreach (var holder in _slotsHolders)
5857
{
5958
// If refcount > 1, it needs to reset the managed slot,
6059
// otherwise it can dealloc without any trick.
61-
if (Runtime.Refcount(type) > 1)
60+
if (holder.Key.Refcount > 1)
6261
{
63-
holder.ResetSlots();
62+
holder.Value.ResetSlots();
6463
}
6564
}
65+
}
66+
67+
foreach (var type in cache.Values)
68+
{
6669
type.Dispose();
6770
}
6871
cache.Clear();
@@ -507,7 +510,7 @@ internal static PyType CreateMetaType(Type impl, out SlotsHolder slotsHolder)
507510
{
508511
throw PythonException.ThrowLastAsClrException();
509512
}
510-
513+
511514
BorrowedReference dict = Util.ReadRef(type, TypeOffset.tp_dict);
512515
using (var mod = Runtime.PyString_FromString("clr._internal"))
513516
Runtime.PyDict_SetItemString(dict, "__module__", mod.Borrow());
@@ -726,6 +729,7 @@ internal static void CopySlot(BorrowedReference from, BorrowedReference to, int
726729

727730
internal static SlotsHolder CreateSlotsHolder(PyType type)
728731
{
732+
type = new PyType(type);
729733
var holder = new SlotsHolder(type);
730734
_slotsHolders.Add(type, holder);
731735
return holder;

src/runtime/Types/ModuleObject.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,6 @@ public static Assembly AddReference(string name)
542542
/// <returns>The Type object</returns>
543543

544544
[ModuleFunction]
545-
[ForbidPythonThreads]
546545
public static Type GetClrType(Type type)
547546
{
548547
return type;

tests/test_engine.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ def test_run_string():
4141
assert sys.multiline_worked == 1
4242

4343
PythonEngine.ReleaseLock()
44+
45+
def test_leak_type():
46+
import clr
47+
sys._leaked_intptr = clr.GetClrType(System.IntPtr)

0 commit comments

Comments
 (0)