Skip to content

Commit 12d59ca

Browse files
lostmsuBadSingleton
authored andcommitted
allocate space for GCHandle in instances of CLR Metatype (which are types themselves)
1 parent a409296 commit 12d59ca

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/runtime/typemanager.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ internal static IntPtr GetTypeHandle(ManagedType obj, Type type)
150150
/// </summary>
151151
internal static IntPtr CreateType(Type impl)
152152
{
153-
IntPtr type = AllocateTypeObject(impl.Name);
153+
IntPtr type = AllocateTypeObject(impl.Name, metatype: Runtime.PyTypeType);
154154
int ob_size = ObjectOffset.Size(type);
155155

156156
// Set tp_basicsize to the size of our managed instance objects.
@@ -223,7 +223,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
223223
base_ = bc.pyHandle;
224224
}
225225

226-
IntPtr type = AllocateTypeObject(name);
226+
IntPtr type = AllocateTypeObject(name, Runtime.PyCLRMetaType);
227227

228228
Marshal.WriteIntPtr(type, TypeOffset.ob_type, Runtime.PyCLRMetaType);
229229
Runtime.XIncref(Runtime.PyCLRMetaType);
@@ -438,12 +438,15 @@ internal static IntPtr CreateMetaType(Type impl, out SlotsHolder slotsHolder)
438438
// the standard type slots, and has to subclass PyType_Type for
439439
// certain functions in the C runtime to work correctly with it.
440440

441-
IntPtr type = AllocateTypeObject("CLR Metatype");
442-
IntPtr py_type = Runtime.PyTypeType;
441+
IntPtr type = AllocateTypeObject("CLR Metatype", metatype: Runtime.PyTypeType);
443442

443+
IntPtr py_type = Runtime.PyTypeType;
444444
Marshal.WriteIntPtr(type, TypeOffset.tp_base, py_type);
445445
Runtime.XIncref(py_type);
446446

447+
int size = TypeOffset.magic() + IntPtr.Size;
448+
Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, new IntPtr(size));
449+
447450
const int flags = TypeFlags.Default
448451
| TypeFlags.Managed
449452
| TypeFlags.HeapType
@@ -536,7 +539,7 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
536539
// Utility to create a subtype of a std Python type, but with
537540
// a managed type able to override implementation
538541

539-
IntPtr type = AllocateTypeObject(name);
542+
IntPtr type = AllocateTypeObject(name, metatype: Runtime.PyTypeType);
540543
//Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, (IntPtr)obSize);
541544
//Marshal.WriteIntPtr(type, TypeOffset.tp_itemsize, IntPtr.Zero);
542545

@@ -582,9 +585,9 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
582585
/// <summary>
583586
/// Utility method to allocate a type object &amp; do basic initialization.
584587
/// </summary>
585-
internal static IntPtr AllocateTypeObject(string name)
588+
internal static IntPtr AllocateTypeObject(string name, IntPtr metatype)
586589
{
587-
IntPtr type = Runtime.PyType_GenericAlloc(Runtime.PyTypeType, 0);
590+
IntPtr type = Runtime.PyType_GenericAlloc(metatype, 0);
588591
// Clr type would not use __slots__,
589592
// and the PyMemberDef after PyHeapTypeObject will have other uses(e.g. type handle),
590593
// thus set the ob_size to 0 for avoiding slots iterations.

0 commit comments

Comments
 (0)