@@ -312,7 +312,7 @@ internal static void Shutdown(ShutdownMode mode)
312
312
ClearClrModules ( ) ;
313
313
RemoveClrRootModule ( ) ;
314
314
315
- MoveClrInstancesOnwershipToPython ( ) ;
315
+ NullGCHandles ( ExtensionType . loadedExtensions ) ;
316
316
ClassManager . RemoveClasses ( ) ;
317
317
TypeManager . RemoveTypes ( ) ;
318
318
@@ -382,7 +382,13 @@ static bool TryCollectingGarbage()
382
382
pyCollected += Finalizer . Instance . DisposeAll ( ) ;
383
383
}
384
384
if ( Volatile . Read ( ref _collected ) == 0 && pyCollected == 0 )
385
+ {
385
386
return true ;
387
+ }
388
+ else
389
+ {
390
+ NullGCHandles ( CLRObject . reflectedObjects ) ;
391
+ }
386
392
}
387
393
return false ;
388
394
}
@@ -521,40 +527,13 @@ private static void PyDictTryDelItem(BorrowedReference dict, string key)
521
527
PyErr_Clear ( ) ;
522
528
}
523
529
524
- private static void MoveClrInstancesOnwershipToPython ( )
530
+ private static void NullGCHandles ( IEnumerable < IntPtr > objects )
525
531
{
526
- foreach ( IntPtr objWithGcHandle in ExtensionType . loadedExtensions
527
- . Concat ( CLRObject . reflectedObjects )
528
- . ToArray ( )
529
- )
532
+ foreach ( IntPtr objWithGcHandle in objects . ToArray ( ) )
530
533
{
531
534
var @ref = new BorrowedReference ( objWithGcHandle ) ;
532
535
ManagedType . TryFreeGCHandle ( @ref ) ;
533
536
}
534
-
535
- //foreach (IntPtr extensionAddr in ExtensionType.loadedExtensions.ToArray())
536
- //{
537
- // var @ref = new BorrowedReference(extensionAddr);
538
- // var type = PyObject_TYPE(@ref);
539
- // //ManagedType.CallTypeClear(@ref, type);
540
- // // obj's tp_type will degenerate to a pure Python type after TypeManager.RemoveTypes(),
541
- // // thus just be safe to give it back to GC chain.
542
- // if (PyVersion >= new Version(3, 9))
543
- // {
544
- // if (!PyObject_GC_IsTracked(@ref))
545
- // {
546
- // PyObject_GC_Track(@ref);
547
- // }
548
- // }
549
- // else
550
- // {
551
- // // in older CPython versions it is safe to call UnTrack any number of time
552
- // // but Track can only be called on something previously untracked
553
- // PyObject_GC_UnTrack(@ref);
554
- // PyObject_GC_Track(@ref);
555
- // }
556
-
557
- //}
558
537
}
559
538
560
539
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
0 commit comments