@@ -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 ;
@@ -828,6 +832,7 @@ public void ResetSlots()
828832 var metatype = Runtime . PyObject_TYPE ( Type ) ;
829833 ManagedType . TryFreeGCHandle ( Type , metatype ) ;
830834 }
835+ Runtime . PyType_Modified ( Type ) ;
831836 }
832837
833838 public static IntPtr GetDefaultSlot ( int offset )
0 commit comments