do not attempt to manually delete derived class instances after TypeManager is terminated and Python is shutting down

This commit is contained in:
Victor Nova 2022-04-07 15:55:58 -07:00
Родитель 5e041afadb
Коммит e9cbb87073
4 изменённых файлов: 18 добавлений и 13 удалений

Просмотреть файл

@ -25,6 +25,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Repo", "Repo", "{441A0123-F
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CI", "CI", "{D301657F-5EAF-4534-B280-B858D651B2E5}"
ProjectSection(SolutionItems) = preProject
.github\workflows\ARM.yml = .github\workflows\ARM.yml
.github\workflows\main.yml = .github\workflows\main.yml
.github\workflows\nuget-preview.yml = .github\workflows\nuget-preview.yml
EndProjectSection

Просмотреть файл

@ -54,8 +54,9 @@ namespace Python.Runtime
}
private static bool _isInitialized = false;
internal static bool IsInitialized => _isInitialized;
private static bool _typesInitialized = false;
internal static bool TypeManagerInitialized => _typesInitialized;
internal static readonly bool Is32Bit = IntPtr.Size == 4;
// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
@ -151,6 +152,7 @@ namespace Python.Runtime
ClassManager.Reset();
ClassDerivedObject.Reset();
TypeManager.Initialize();
_typesInitialized = true;
// Initialize modules that depend on the runtime class.
AssemblyManager.Initialize();
@ -272,6 +274,7 @@ namespace Python.Runtime
NullGCHandles(ExtensionType.loadedExtensions);
ClassManager.RemoveClasses();
TypeManager.RemoveTypes();
_typesInitialized = false;
MetaType.Release();
PyCLRMetaType.Dispose();
@ -291,9 +294,10 @@ namespace Python.Runtime
Finalizer.Shutdown();
InternString.Shutdown();
ResetPyMembers();
if (!HostedInPython)
{
ResetPyMembers();
GC.Collect();
GC.WaitForPendingFinalizers();
PyGILState_Release(state);
@ -310,7 +314,6 @@ namespace Python.Runtime
}
else
{
ResetPyMembers();
PyGILState_Release(state);
}
}

Просмотреть файл

@ -832,6 +832,7 @@ namespace Python.Runtime
var metatype = Runtime.PyObject_TYPE(Type);
ManagedType.TryFreeGCHandle(Type, metatype);
}
Runtime.PyType_Modified(Type);
}
public static IntPtr GetDefaultSlot(int offset)

Просмотреть файл

@ -59,10 +59,7 @@ namespace Python.Runtime
// Decrement the python object's reference count.
// This doesn't actually destroy the object, it just sets the reference to this object
// to be a weak reference and it will be destroyed when the C# object is destroyed.
if (!self.IsNull())
{
Runtime.XDecref(self.Steal());
}
Runtime.XDecref(self.Steal());
return Converter.ToPython(obj, type.Value);
}
@ -942,13 +939,16 @@ namespace Python.Runtime
var type = Runtime.PyObject_TYPE(@ref.Borrow());
// rare case when it's needed
// matches correspdonging PyObject_GC_UnTrack
// in ClassDerivedObject.tp_dealloc
Runtime.PyObject_GC_Del(@ref.Steal());
if (!Runtime.HostedInPython || Runtime.TypeManagerInitialized)
{
// rare case when it's needed
// matches correspdonging PyObject_GC_UnTrack
// in ClassDerivedObject.tp_dealloc
Runtime.PyObject_GC_Del(@ref.Steal());
// must decref our type
Runtime.XDecref(StolenReference.DangerousFromPointer(type.DangerousGetAddress()));
// must decref our type
Runtime.XDecref(StolenReference.DangerousFromPointer(type.DangerousGetAddress()));
}
}
internal static FieldInfo? GetPyObjField(Type type) => type.GetField(PyObjName, PyObjFlags);