Skip to content
Closed
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
  • Loading branch information
thesn10 committed May 23, 2020
commit 52cc24ccdbf11decd176875ccfd54cf1ef9f0bab
42 changes: 40 additions & 2 deletions src/runtime/pybuffer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -214,8 +215,45 @@ public int Read(byte[] buffer, int offset, int count) {

private void Dispose(bool disposing)
{
if (!disposedValue) {
Runtime.PyBuffer_Release(ref _view);
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);
}
}

_exporter = null;
Shape = null;
Expand Down