Browse Source

DisposableObject: Added CheckDisposed()

tags/v0.12
Eli Belash 6 years ago
parent
commit
71c592ceaf
1 changed files with 14 additions and 0 deletions
  1. +14
    -0
      src/TensorFlowNET.Core/DisposableObject.cs

+ 14
- 0
src/TensorFlowNET.Core/DisposableObject.cs View File

@@ -17,6 +17,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;


namespace Tensorflow namespace Tensorflow
@@ -36,6 +37,7 @@ namespace Tensorflow
protected DisposableObject(IntPtr handle) protected DisposableObject(IntPtr handle)
=> _handle = handle; => _handle = handle;


[SuppressMessage("ReSharper", "InvertIf")]
private void internal_dispose(bool disposing) private void internal_dispose(bool disposing)
{ {
if (_disposed) if (_disposed)
@@ -48,6 +50,7 @@ namespace Tensorflow
// dispose managed state (managed objects). // dispose managed state (managed objects).
DisposeManagedResources(); DisposeManagedResources();


//free unmanaged memory
if (_handle != IntPtr.Zero) if (_handle != IntPtr.Zero)
{ {
DisposeUnmanagedResources(_handle); DisposeUnmanagedResources(_handle);
@@ -79,5 +82,16 @@ namespace Tensorflow


GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }

/// <summary>
/// If <see cref="_handle"/> is <see cref="IntPtr.Zero"/> then throws <see cref="ObjectDisposedException"/>
/// </summary>
/// <exception cref="ObjectDisposedException">When <see cref="_handle"/> is <see cref="IntPtr.Zero"/></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void EnsureNotDisposed()
{
if (_disposed)
throw new ObjectDisposedException($"Unable to access disposed object, Type: {GetType().Name}");
}
} }
} }

Loading…
Cancel
Save