7
7
8
8
namespace Python . Runtime
9
9
{
10
- public sealed class PyBuffer : IDisposable
10
+ public sealed class PyBuffer : IPyDisposable
11
11
{
12
12
private PyObject _exporter ;
13
13
private Py_buffer _view ;
@@ -217,43 +217,11 @@ private void Dispose(bool disposing)
217
217
{
218
218
if ( ! disposedValue )
219
219
{
220
- Debug . Assert ( _view . obj != IntPtr . Zero , "Buffer object is invalid (no exporter object ref)" ) ;
221
- //if (_view.obj == IntPtr.Zero)
222
- //{
223
- // return;
224
- //}
225
-
226
220
if ( Runtime . Py_IsInitialized ( ) == 0 )
227
221
throw new InvalidOperationException ( "Python runtime must be initialized" ) ;
228
222
229
- if ( ! Runtime . IsFinalizing )
230
- {
231
- long refcount = Runtime . Refcount ( _view . obj ) ;
232
- Debug . Assert ( refcount > 0 , "Object refcount is 0 or less" ) ;
233
-
234
- if ( refcount == 1 )
235
- {
236
- Runtime . PyErr_Fetch ( out var errType , out var errVal , out var traceback ) ;
237
-
238
- try
239
- {
240
- // this also decrements ref count for _view->obj
241
- Runtime . PyBuffer_Release ( ref _view ) ;
242
- Runtime . CheckExceptionOccurred ( ) ;
243
- }
244
- finally
245
- {
246
- // Python requires finalizers to preserve exception:
247
- // https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation
248
- Runtime . PyErr_Restore ( errType , errVal , traceback ) ;
249
- }
250
- }
251
- else
252
- {
253
- // this also decrements ref count for _view->obj
254
- Runtime . PyBuffer_Release ( ref _view ) ;
255
- }
256
- }
223
+ // this also decrements ref count for _view->obj
224
+ Runtime . PyBuffer_Release ( ref _view ) ;
257
225
258
226
_exporter = null ;
259
227
Shape = null ;
@@ -266,7 +234,11 @@ private void Dispose(bool disposing)
266
234
267
235
~ PyBuffer ( )
268
236
{
269
- Dispose ( false ) ;
237
+ if ( disposedValue )
238
+ {
239
+ return ;
240
+ }
241
+ Finalizer . Instance . AddFinalizedObject ( this ) ;
270
242
}
271
243
272
244
/// <summary>
@@ -278,5 +250,10 @@ public void Dispose()
278
250
Dispose ( true ) ;
279
251
GC . SuppressFinalize ( this ) ;
280
252
}
253
+
254
+ public IntPtr [ ] GetTrackedHandles ( )
255
+ {
256
+ return new IntPtr [ ] { _view . obj } ;
257
+ }
281
258
}
282
259
}
0 commit comments