|
| 1 | +/***************************************************************************** |
| 2 | + Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +******************************************************************************/ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Text; |
| 20 | + |
| 21 | +namespace Tensorflow |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Abstract class for disposable object allocated in unmanaged runtime. |
| 25 | + /// </summary> |
| 26 | + public abstract class DisposableObject : IDisposable |
| 27 | + { |
| 28 | + protected IntPtr _handle; |
| 29 | + |
| 30 | + protected DisposableObject() { } |
| 31 | + |
| 32 | + public DisposableObject(IntPtr handle) |
| 33 | + { |
| 34 | + _handle = handle; |
| 35 | + } |
| 36 | + |
| 37 | + private bool disposedValue = false; // To detect redundant calls |
| 38 | + |
| 39 | + protected virtual void DisposeManagedState() |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + protected abstract void DisposeUnManagedState(); |
| 44 | + |
| 45 | + protected virtual void Dispose(bool disposing) |
| 46 | + { |
| 47 | + if (!disposedValue) |
| 48 | + { |
| 49 | + if (disposing) |
| 50 | + { |
| 51 | + // dispose managed state (managed objects). |
| 52 | + DisposeManagedState(); |
| 53 | + } |
| 54 | + |
| 55 | + // free unmanaged resources (unmanaged objects) and override a finalizer below. |
| 56 | + /*IntPtr h = IntPtr.Zero; |
| 57 | + lock (this) |
| 58 | + { |
| 59 | + h = _handle; |
| 60 | + _handle = IntPtr.Zero; |
| 61 | + }*/ |
| 62 | + if (_handle != IntPtr.Zero) |
| 63 | + DisposeUnManagedState(); |
| 64 | + |
| 65 | + // set large fields to null. |
| 66 | + _handle = IntPtr.Zero; |
| 67 | + |
| 68 | + disposedValue = true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. |
| 73 | + ~DisposableObject() |
| 74 | + { |
| 75 | + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. |
| 76 | + Dispose(false); |
| 77 | + } |
| 78 | + |
| 79 | + // This code added to correctly implement the disposable pattern. |
| 80 | + public void Dispose() |
| 81 | + { |
| 82 | + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. |
| 83 | + Dispose(true); |
| 84 | + // uncomment the following line if the finalizer is overridden above. |
| 85 | + GC.SuppressFinalize(this); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments