@@ -56,6 +56,10 @@ public static void Reset()
5656
5757 internal static void RemoveClasses ( )
5858 {
59+ foreach ( var @class in cache . Values )
60+ {
61+ @class . Dispose ( ) ;
62+ }
5963 cache . Clear ( ) ;
6064 }
6165
@@ -81,11 +85,6 @@ internal static ClassManagerState SaveRuntimeData()
8185 var contexts = new Dictionary < ReflectedClrType , InterDomainContext > ( ) ;
8286 foreach ( var cls in cache )
8387 {
84- if ( ! cls . Key . Valid )
85- {
86- // Don't serialize an invalid class
87- continue ;
88- }
8988 var context = contexts [ cls . Value ] = new InterDomainContext ( ) ;
9089 var cb = ( ClassBase ) ManagedType . GetManagedObject ( cls . Value ) ! ;
9190 cb . Save ( cls . Value , context ) ;
@@ -129,31 +128,32 @@ internal static void RestoreRuntimeData(ClassManagerState storage)
129128 var contexts = storage . Contexts ;
130129 foreach ( var pair in cache )
131130 {
132- if ( ! pair . Key . Valid )
131+ var context = contexts [ pair . Value ] ;
132+ if ( pair . Key . Valid )
133+ {
134+ pair . Value . Restore ( context ) ;
135+ }
136+ else
133137 {
134138 invalidClasses . Add ( pair ) ;
135- continue ;
139+ var cb = new UnloadedClass ( pair . Key . Name ) ;
140+ cb . Load ( pair . Value , context ) ;
141+ pair . Value . Restore ( cb ) ;
136142 }
137-
138- pair . Value . Restore ( contexts [ pair . Value ] ) ;
139- }
140-
141- foreach ( var pair in invalidClasses )
142- {
143- cache . Remove ( pair . Key ) ;
144- pair . Value . Dispose ( ) ;
145143 }
146144 }
147145
148146 /// <summary>
149147 /// Return the ClassBase-derived instance that implements a particular
150148 /// reflected managed type, creating it if it doesn't yet exist.
151149 /// </summary>
152- internal static ReflectedClrType GetClass ( Type type ) => ReflectedClrType . GetOrCreate ( type , out _ ) ;
150+ internal static ReflectedClrType GetClass ( Type type ) => ReflectedClrType . GetOrCreate ( type ) ;
153151 internal static ClassBase GetClassImpl ( Type type )
154152 {
155- ReflectedClrType . GetOrCreate ( type , out var cb ) ;
156- return cb ;
153+ var pyType = GetClass ( type ) ;
154+ var impl = ( ClassBase ) ManagedType . GetManagedObject ( pyType ) ! ;
155+ Debug . Assert ( impl is not null ) ;
156+ return impl ! ;
157157 }
158158
159159
@@ -236,22 +236,11 @@ internal static void InitClassBase(Type type, ClassBase impl, PyType pyType)
236236 var item = iter . Value ;
237237 var name = iter . Key ;
238238 impl . dotNetMembers . Add ( name ) ;
239- switch ( item )
240- {
241- case ClassBase nestedClass :
242- Runtime . PyDict_SetItemString ( dict , name , GetClass ( nestedClass . type . Value ) ) ;
243- break ;
244- case ExtensionType extension :
245- using ( var pyRef = extension . Alloc ( ) )
246- {
247- Runtime . PyDict_SetItemString ( dict , name , pyRef . Borrow ( ) ) ;
248- }
249- break ;
250- default :
251- throw new NotSupportedException ( ) ;
252- }
239+ Runtime . PyDict_SetItemString ( dict , name , item ) ;
253240 if ( ClassBase . CilToPyOpMap . TryGetValue ( name , out var pyOp )
254- && item is MethodObject method )
241+ // workaround for unintialized types crashing in GetManagedObject
242+ && item is not ReflectedClrType
243+ && ManagedType . GetManagedObject ( item ) is MethodObject method )
255244 {
256245 impl . richcompare . Add ( pyOp , method ) ;
257246 }
@@ -347,7 +336,7 @@ private static ClassInfo GetClassInfo(Type type)
347336 var ci = new ClassInfo ( ) ;
348337 var methods = new Dictionary < string , List < MethodInfo > > ( ) ;
349338 MethodInfo meth ;
350- ManagedType ob ;
339+ ExtensionType ob ;
351340 string name ;
352341 Type tp ;
353342 int i , n ;
@@ -472,7 +461,7 @@ private static ClassInfo GetClassInfo(Type type)
472461 }
473462
474463 ob = new PropertyObject ( pi ) ;
475- ci . members [ pi . Name ] = ob ;
464+ ci . members [ pi . Name ] = ob . AllocObject ( ) ;
476465 continue ;
477466
478467 case MemberTypes . Field :
@@ -482,7 +471,7 @@ private static ClassInfo GetClassInfo(Type type)
482471 continue ;
483472 }
484473 ob = new FieldObject ( fi ) ;
485- ci . members [ mi . Name ] = ob ;
474+ ci . members [ mi . Name ] = ob . AllocObject ( ) ;
486475 continue ;
487476
488477 case MemberTypes . Event :
@@ -494,7 +483,7 @@ private static ClassInfo GetClassInfo(Type type)
494483 ob = ei . AddMethod . IsStatic
495484 ? new EventBinding ( ei )
496485 : new EventObject ( ei ) ;
497- ci . members [ ei . Name ] = ob ;
486+ ci . members [ ei . Name ] = ob . AllocObject ( ) ;
498487 continue ;
499488
500489 case MemberTypes . NestedType :
@@ -506,9 +495,8 @@ private static ClassInfo GetClassInfo(Type type)
506495 }
507496 // Note the given instance might be uninitialized
508497 var pyType = GetClass ( tp ) ;
509- ob = ManagedType . GetManagedObject ( pyType ) ! ;
510- Debug . Assert ( ob is not null ) ;
511- ci . members [ mi . Name ] = ob ;
498+ // make a copy, that could be disposed later
499+ ci . members [ mi . Name ] = new ReflectedClrType ( pyType ) ;
512500 continue ;
513501 }
514502 }
@@ -519,18 +507,18 @@ private static ClassInfo GetClassInfo(Type type)
519507 var mlist = iter . Value . ToArray ( ) ;
520508
521509 ob = new MethodObject ( type , name , mlist ) ;
522- ci . members [ name ] = ob ;
510+ ci . members [ name ] = ob . AllocObject ( ) ;
523511 if ( mlist . Any ( OperatorMethod . IsOperatorMethod ) )
524512 {
525513 string pyName = OperatorMethod . GetPyMethodName ( name ) ;
526514 string pyNameReverse = OperatorMethod . ReversePyMethodName ( pyName ) ;
527515 OperatorMethod . FilterMethods ( mlist , out var forwardMethods , out var reverseMethods ) ;
528516 // Only methods where the left operand is the declaring type.
529517 if ( forwardMethods . Length > 0 )
530- ci . members [ pyName ] = new MethodObject ( type , name , forwardMethods ) ;
518+ ci . members [ pyName ] = new MethodObject ( type , name , forwardMethods ) . AllocObject ( ) ;
531519 // Only methods where only the right operand is the declaring type.
532520 if ( reverseMethods . Length > 0 )
533- ci . members [ pyNameReverse ] = new MethodObject ( type , name , reverseMethods ) ;
521+ ci . members [ pyNameReverse ] = new MethodObject ( type , name , reverseMethods ) . AllocObject ( ) ;
534522 }
535523 }
536524
@@ -563,7 +551,7 @@ private static ClassInfo GetClassInfo(Type type)
563551 private class ClassInfo
564552 {
565553 public Indexer ? indexer ;
566- public readonly Dictionary < string , ManagedType > members = new ( ) ;
554+ public readonly Dictionary < string , PyObject > members = new ( ) ;
567555
568556 internal ClassInfo ( )
569557 {
0 commit comments