Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update finalizer 2
  • Loading branch information
thesn10 authored and koubaa committed Aug 1, 2020
commit 8237e87b2c396d3cd31b2934fb7cb5947872a22f
49 changes: 13 additions & 36 deletions src/runtime/pybuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Python.Runtime
{
public sealed class PyBuffer : IDisposable
public sealed class PyBuffer : IPyDisposable
{
private PyObject _exporter;
private Py_buffer _view;
Expand Down Expand Up @@ -217,43 +217,11 @@ private void Dispose(bool disposing)
{
if (!disposedValue)
{
Debug.Assert(_view.obj != IntPtr.Zero, "Buffer object is invalid (no exporter object ref)");
//if (_view.obj == IntPtr.Zero)
//{
// return;
//}

if (Runtime.Py_IsInitialized() == 0)
throw new InvalidOperationException("Python runtime must be initialized");

if (!Runtime.IsFinalizing)
{
long refcount = Runtime.Refcount(_view.obj);
Debug.Assert(refcount > 0, "Object refcount is 0 or less");

if (refcount == 1)
{
Runtime.PyErr_Fetch(out var errType, out var errVal, out var traceback);

try
{
// this also decrements ref count for _view->obj
Runtime.PyBuffer_Release(ref _view);
Runtime.CheckExceptionOccurred();
}
finally
{
// Python requires finalizers to preserve exception:
// https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation
Runtime.PyErr_Restore(errType, errVal, traceback);
}
}
else
{
// this also decrements ref count for _view->obj
Runtime.PyBuffer_Release(ref _view);
}
}
// this also decrements ref count for _view->obj
Runtime.PyBuffer_Release(ref _view);

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

~PyBuffer()
{
Dispose(false);
if (disposedValue)
{
return;
}
Finalizer.Instance.AddFinalizedObject(this);
}

/// <summary>
Expand All @@ -278,5 +250,10 @@ public void Dispose()
Dispose(true);
GC.SuppressFinalize(this);
}

public IntPtr[] GetTrackedHandles()
{
return new IntPtr[] { _view.obj };
}
}
}