File tree Expand file tree Collapse file tree 2 files changed +13
-19
lines changed
Expand file tree Collapse file tree 2 files changed +13
-19
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ public void CollectBasicObject()
4545 called = true ;
4646 } ;
4747
48+ Assert . IsFalse ( called ) ;
49+ Finalizer . Instance . CollectOnce += handler ;
50+
4851 WeakReference shortWeak ;
4952 WeakReference longWeak ;
5053 {
@@ -60,12 +63,9 @@ public void CollectBasicObject()
6063 Assert . NotZero ( garbage . Count ) ;
6164 Assert . IsTrue ( garbage . Any ( T => ReferenceEquals ( T . Target , longWeak . Target ) ) ) ;
6265 }
63-
64- Assert . IsFalse ( called ) ;
65- Finalizer . Instance . CollectOnce += handler ;
6666 try
6767 {
68- Finalizer . Instance . CallPendingFinalizers ( ) ;
68+ Finalizer . Instance . Collect ( forceDispose : false ) ;
6969 }
7070 finally
7171 {
Original file line number Diff line number Diff line change @@ -75,22 +75,19 @@ private Finalizer()
7575 Threshold = 200 ;
7676 }
7777
78- public bool CallPendingFinalizers ( )
78+ public void Collect ( bool forceDispose = true )
7979 {
8080 if ( Instance . _finalizerTask != null
8181 && ! Instance . _finalizerTask . IsCompleted )
8282 {
83- var ts = PythonEngine . BeginAllowThreads ( ) ;
84- Instance . _finalizerTask . Wait ( ) ;
85- PythonEngine . EndAllowThreads ( ts ) ;
86- return true ;
83+ using ( Py . GIL ( ) )
84+ {
85+ var ts = PythonEngine . BeginAllowThreads ( ) ;
86+ Instance . _finalizerTask . Wait ( ) ;
87+ PythonEngine . EndAllowThreads ( ts ) ;
88+ }
8789 }
88- return false ;
89- }
90-
91- public void Collect ( )
92- {
93- if ( ! Instance . CallPendingFinalizers ( ) )
90+ else if ( forceDispose )
9491 {
9592 Instance . DisposeAll ( ) ;
9693 }
@@ -133,10 +130,7 @@ internal static void Shutdown()
133130 Instance . _objQueue = new ConcurrentQueue < IPyDisposable > ( ) ;
134131 return ;
135132 }
136- if ( ! Instance . CallPendingFinalizers ( ) )
137- {
138- Instance . DisposeAll ( ) ;
139- }
133+ Instance . Collect ( forceDispose : true ) ;
140134 }
141135
142136 private void AddPendingCollect ( )
You can’t perform that action at this time.
0 commit comments