Browse Source

DisposableObject: Revamped based on VS unmanaged resource pattern.

tags/v0.12
Eli Belash 6 years ago
parent
commit
93d7e9a669
1 changed files with 20 additions and 19 deletions
  1. +20
    -19
      src/TensorFlowNET.Core/DisposableObject.cs

+ 20
- 19
src/TensorFlowNET.Core/DisposableObject.cs View File

@@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace Tensorflow
@@ -26,27 +27,32 @@ namespace Tensorflow
public abstract class DisposableObject : IDisposable
{
protected IntPtr _handle;
protected bool _disposed;

protected DisposableObject() { }
[SuppressMessage("ReSharper", "UnusedMember.Global")]
protected DisposableObject()
{ }

protected DisposableObject(IntPtr handle)
protected DisposableObject(IntPtr handle)
=> _handle = handle;

private void internal_dispose(bool disposing)
{
if (_disposed)
return;

_disposed = true;

//first handle managed, they might use the unmanaged resources.
if (disposing)
{
// free unmanaged resources (unmanaged objects) and override a finalizer below.
if (_handle != IntPtr.Zero)
{
// dispose managed state (managed objects).
DisposeManagedResources();
// dispose managed state (managed objects).
DisposeManagedResources();

// set large fields to null.
DisposeUnmanagedResources(_handle);
if (_handle != IntPtr.Zero)
{
DisposeUnmanagedResources(_handle);

_handle = IntPtr.Zero;
}
_handle = IntPtr.Zero;
}
}

@@ -55,27 +61,22 @@ namespace Tensorflow
/// </summary>
/// <remarks>Equivalent to what you would perform inside <see cref="Dispose()"/></remarks>
protected virtual void DisposeManagedResources()
{
}
{ }

/// <summary>
/// Dispose any unmanaged resources related to given <paramref name="handle"/>.
/// </summary>
protected abstract void DisposeUnmanagedResources(IntPtr handle);

// override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
~DisposableObject()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
internal_dispose(false);
}

// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
internal_dispose(true);
// uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);
}
}

Loading…
Cancel
Save