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

75 lines
2.5 KiB

  1. //----------------------------------------------------------------------------
  2. // Copyright (C) 2004-2025 by EMGU Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. using System;
  5. namespace Emgu.Util
  6. {
  7. /// <summary>
  8. /// An abstract class that wrap around a disposable object
  9. /// </summary>
  10. public abstract class DisposableObject : IDisposable
  11. {
  12. /// <summary> Track whether Dispose has been called. </summary>
  13. private bool _disposed;
  14. /// <summary>
  15. /// The dispose function that implements IDisposable interface
  16. /// </summary>
  17. public void Dispose()
  18. {
  19. Dispose(true);
  20. GC.SuppressFinalize(this);
  21. }
  22. /// <summary>
  23. /// Dispose(bool disposing) executes in two distinct scenarios.
  24. /// If disposing equals true, the method has been called directly
  25. /// or indirectly by a user's code. Managed and unmanaged resources
  26. /// can be disposed.
  27. /// If disposing equals false, the method has been called by the
  28. /// runtime from inside the finalizer and you should not reference
  29. /// other objects. Only unmanaged resources can be disposed.
  30. /// </summary>
  31. /// <param name="disposing"> If disposing equals false, the method has been called by the runtime from inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed. </param>
  32. private void Dispose(bool disposing)
  33. {
  34. // Check to see if Dispose has already been called.
  35. if (!_disposed)
  36. {
  37. _disposed = true;
  38. // If disposing equals true, release all managed resources as well
  39. if (disposing)
  40. {
  41. ReleaseManagedResources();
  42. }
  43. //release unmanaged resource.
  44. DisposeObject();
  45. }
  46. }
  47. /// <summary>
  48. /// Release the managed resources. This function will be called during the disposal of the current object.
  49. /// override ride this function if you need to call the Dispose() function on any managed IDisposable object created by the current object
  50. /// </summary>
  51. protected virtual void ReleaseManagedResources()
  52. {
  53. }
  54. /// <summary>
  55. /// Release the unmanaged resources
  56. /// </summary>
  57. protected abstract void DisposeObject();
  58. /// <summary>
  59. /// Destructor
  60. /// </summary>
  61. ~DisposableObject()
  62. {
  63. Dispose(false);
  64. }
  65. }
  66. }