You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DisposableObject.cs 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Diagnostics.CodeAnalysis;
  15. using System.Runtime.CompilerServices;
  16. using Tensorflow.Train;
  17. namespace Tensorflow
  18. {
  19. /// <summary>
  20. /// Abstract class for disposable object allocated in unmanaged runtime.
  21. /// https://docs.microsoft.com/en-us/dotnet/api/system.idisposable.dispose?redirectedfrom=MSDN&view=net-5.0#System_IDisposable_Dispose
  22. /// </summary>
  23. public abstract class DisposableObject : IDisposable
  24. {
  25. protected IntPtr _handle;
  26. protected bool _disposed;
  27. protected DisposableObject()
  28. { }
  29. protected DisposableObject(IntPtr handle)
  30. => _handle = handle;
  31. private void Dispose(bool disposing)
  32. {
  33. if (_disposed)
  34. return;
  35. //first handle managed, they might use the unmanaged resources.
  36. if (disposing)
  37. {
  38. // dispose managed state (managed objects).
  39. DisposeManagedResources();
  40. }
  41. // free unmanaged memory
  42. if (_handle != IntPtr.Zero)
  43. {
  44. // Call the appropriate methods to clean up
  45. // unmanaged resources here.
  46. // If disposing is false,
  47. // only the following code is executed.
  48. DisposeUnmanagedResources(_handle);
  49. _handle = IntPtr.Zero;
  50. }
  51. // Note disposing has been done.
  52. _disposed = true;
  53. }
  54. /// <summary>
  55. /// Dispose any managed resources.
  56. /// </summary>
  57. /// <remarks>Equivalent to what you would perform inside <see cref="Dispose()"/></remarks>
  58. protected virtual void DisposeManagedResources()
  59. { }
  60. /// <summary>
  61. /// Dispose any unmanaged resources related to given <paramref name="handle"/>.
  62. /// </summary>
  63. protected abstract void DisposeUnmanagedResources(IntPtr handle);
  64. public void Dispose()
  65. {
  66. Dispose(true);
  67. // This object will be cleaned up by the Dispose method.
  68. // Therefore, you should call GC.SupressFinalize to
  69. // take this object off the finalization queue
  70. // and prevent finalization code for this object
  71. // from executing a second time.
  72. GC.SuppressFinalize(this);
  73. }
  74. ~DisposableObject()
  75. {
  76. Dispose(false);
  77. }
  78. }
  79. public abstract class DisposableTrackableObject: Trackable, IDisposable
  80. {
  81. protected IntPtr _handle;
  82. protected bool _disposed;
  83. protected DisposableTrackableObject()
  84. { }
  85. protected DisposableTrackableObject(IntPtr handle)
  86. => _handle = handle;
  87. private void Dispose(bool disposing)
  88. {
  89. if (_disposed)
  90. return;
  91. //first handle managed, they might use the unmanaged resources.
  92. if (disposing)
  93. {
  94. // dispose managed state (managed objects).
  95. DisposeManagedResources();
  96. }
  97. // free unmanaged memory
  98. if (_handle != IntPtr.Zero)
  99. {
  100. // Call the appropriate methods to clean up
  101. // unmanaged resources here.
  102. // If disposing is false,
  103. // only the following code is executed.
  104. DisposeUnmanagedResources(_handle);
  105. _handle = IntPtr.Zero;
  106. }
  107. // Note disposing has been done.
  108. _disposed = true;
  109. }
  110. /// <summary>
  111. /// Dispose any managed resources.
  112. /// </summary>
  113. /// <remarks>Equivalent to what you would perform inside <see cref="Dispose()"/></remarks>
  114. protected virtual void DisposeManagedResources()
  115. { }
  116. /// <summary>
  117. /// Dispose any unmanaged resources related to given <paramref name="handle"/>.
  118. /// </summary>
  119. protected abstract void DisposeUnmanagedResources(IntPtr handle);
  120. public void Dispose()
  121. {
  122. Dispose(true);
  123. // This object will be cleaned up by the Dispose method.
  124. // Therefore, you should call GC.SupressFinalize to
  125. // take this object off the finalization queue
  126. // and prevent finalization code for this object
  127. // from executing a second time.
  128. GC.SuppressFinalize(this);
  129. }
  130. ~DisposableTrackableObject()
  131. {
  132. Dispose(false);
  133. }
  134. }
  135. }