Skip to content

Commit 92cde67

Browse files
committed
allocate space for GCHandle in instances of CLR Metatype (which are types themselves)
1 parent 2450cce commit 92cde67

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
@@ -143,7 +143,7 @@ internal static IntPtr GetTypeHandle(ManagedType obj, Type type)
143143
/// </summary>
144144
internal static IntPtr CreateType(Type impl)
145145
{
146-
IntPtr type = AllocateTypeObject(impl.Name);
146+
IntPtr type = AllocateTypeObject(impl.Name, metatype: Runtime.PyTypeType);
147147
int ob_size = ObjectOffset.Size(type);
148148

149149
// Set tp_basicsize to the size of our managed instance objects.
@@ -212,7 +212,7 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
212212
base_ = bc.pyHandle;
213213
}
214214

215-
IntPtr type = AllocateTypeObject(name);
215+
IntPtr type = AllocateTypeObject(name, Runtime.PyCLRMetaType);
216216

217217
Marshal.WriteIntPtr(type, TypeOffset.ob_type, Runtime.PyCLRMetaType);
218218
Runtime.XIncref(Runtime.PyCLRMetaType);
@@ -443,12 +443,15 @@ internal static IntPtr CreateMetaType(Type impl, out SlotsHolder slotsHolder)
443443
// the standard type slots, and has to subclass PyType_Type for
444444
// certain functions in the C runtime to work correctly with it.
445445

446-
IntPtr type = AllocateTypeObject("CLR Metatype");
447-
IntPtr py_type = Runtime.PyTypeType;
446+
IntPtr type = AllocateTypeObject("CLR Metatype", metatype: Runtime.PyTypeType);
448447

448+
IntPtr py_type = Runtime.PyTypeType;
449449
Marshal.WriteIntPtr(type, TypeOffset.tp_base, py_type);
450450
Runtime.XIncref(py_type);
451451

452+
int size = TypeOffset.magic() + IntPtr.Size;
453+
Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, new IntPtr(size));
454+
452455
const int flags = TypeFlags.Default
453456
| TypeFlags.Managed
454457
| TypeFlags.HeapType
@@ -538,7 +541,7 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
538541
// Utility to create a subtype of a std Python type, but with
539542
// a managed type able to override implementation
540543

541-
IntPtr type = AllocateTypeObject(name);
544+
IntPtr type = AllocateTypeObject(name, metatype: Runtime.PyTypeType);
542545
//Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, (IntPtr)obSize);
543546
//Marshal.WriteIntPtr(type, TypeOffset.tp_itemsize, IntPtr.Zero);
544547

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

0 commit comments

Comments
 (0)