Guest User

Untitled

a guest
Jan 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. public class Resource : IDisposable
  2. {
  3. private IntPtr nativeResource = Marshal.AllocHGlobal(100);
  4. private AnotherResource managedResource = new AnotherResource();
  5.  
  6. // Dispose() calls Dispose(true)
  7. public void Dispose()
  8. {
  9. Dispose(true);
  10. GC.SuppressFinalize(this);
  11. }
  12. // NOTE: Leave out the finalizer altogether if this class doesn't
  13. // own unmanaged resources itself, but leave the other methods
  14. // exactly as they are.
  15. ~Resource()
  16. {
  17. // Finalizer calls Dispose(false)
  18. Dispose(false);
  19. }
  20. // The bulk of the clean-up code is implemented in Dispose(bool)
  21. protected virtual void Dispose(bool disposing)
  22. {
  23. if (disposing)
  24. {
  25. // free managed resources
  26. if (managedResource != null)
  27. {
  28. managedResource.Dispose();
  29. managedResource = null;
  30. }
  31. }
  32. // free native resources if there are any.
  33. if (nativeResource != IntPtr.Zero)
  34. {
  35. Marshal.FreeHGlobal(nativeResource);
  36. nativeResource = IntPtr.Zero;
  37. }
  38. }
  39. }
Add Comment
Please, Sign In to add comment