@@ -69,19 +69,23 @@ internal ClassDerivedObject(Type tp) : base(tp)
69
69
70
70
public new static void tp_dealloc ( NewReference ob )
71
71
{
72
- var self = ( CLRObject ) GetManagedObject ( ob . Borrow ( ) ) ! ;
72
+ var self = ( CLRObject ? ) GetManagedObject ( ob . Borrow ( ) ) ;
73
73
74
74
// don't let the python GC destroy this object
75
75
Runtime . PyObject_GC_UnTrack ( ob . Borrow ( ) ) ;
76
76
77
- // The python should now have a ref count of 0, but we don't actually want to
78
- // deallocate the object until the C# object that references it is destroyed.
79
- // So we don't call PyObject_GC_Del here and instead we set the python
80
- // reference to a weak reference so that the C# object can be collected.
81
- GCHandle oldHandle = GetGCHandle ( ob . Borrow ( ) ) ;
82
- GCHandle gc = GCHandle . Alloc ( self , GCHandleType . Weak ) ;
83
- SetGCHandle ( ob . Borrow ( ) , gc ) ;
84
- oldHandle . Free ( ) ;
77
+ // self may be null after Shutdown begun
78
+ if ( self is not null )
79
+ {
80
+ // The python should now have a ref count of 0, but we don't actually want to
81
+ // deallocate the object until the C# object that references it is destroyed.
82
+ // So we don't call PyObject_GC_Del here and instead we set the python
83
+ // reference to a weak reference so that the C# object can be collected.
84
+ GCHandle oldHandle = GetGCHandle ( ob . Borrow ( ) ) ;
85
+ GCHandle gc = GCHandle . Alloc ( self , GCHandleType . Weak ) ;
86
+ SetGCHandle ( ob . Borrow ( ) , gc ) ;
87
+ oldHandle . Free ( ) ;
88
+ }
85
89
}
86
90
87
91
/// <summary>
0 commit comments