8000 Update finalizer 2 · pythonnet/pythonnet@530e8c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 530e8c7

Browse files
committed
Update finalizer 2
1 parent 52cc24c commit 530e8c7

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

src/runtime/pybuffer.cs

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Python.Runtime
99
{
10-
public sealed class PyBuffer : IDisposable
10+
public sealed class PyBuffer : IPyDisposable
1111
{
1212
private PyObject _exporter;
1313
private Py_buffer _view;
@@ -217,43 +217,11 @@ private void Dispose(bool disposing)
217217
{
218218
if (!disposedValue)
219219
{
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-
226220
if (Runtime.Py_IsInitialized() == 0)
227221
throw new InvalidOperationException("Python runtime must be initialized");
228222

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);
257225

258226
_exporter = null;
259227
Shape = null;
@@ -266,7 +234,11 @@ private void Dispose(bool disposing)
266234

267235
~PyBuffer()
268236
{
269-
Dispose(false);
237+
if (disposedValue)
238+
{
239+
return;
240+
}
241+
Finalizer.Instance.AddFinalizedObject(this);
270242
}
271243

272244
/// <summary>
@@ -278,5 +250,10 @@ public void Dispose()
278250
Dispose(true);
279251
GC.SuppressFinalize(this);
280252
}
253+
254+
public IntPtr[] GetTrackedHandles()
255+
{
256+
return new IntPtr[] { _view.obj };
257+
}
281258
}
282259
}

0 commit comments

Comments
 (0)
0