Advertisement
Guest User

Untitled

a guest
May 24th, 2011
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1.     internal class DisposableIntPtr : IDisposable {
  2.         IntPtr ptr;
  3.         int _size;
  4.  
  5.         public DisposableIntPtr(int size) {
  6.             ptr = Marshal.AllocHGlobal(size);
  7.             _size = size;
  8.         }
  9.  
  10.         public void WriteInt32( int val )
  11.         {
  12.             if (_size == sizeof(Int32))
  13.             {
  14.                 Marshal.WriteInt32(ptr, val);
  15.             }
  16.             else if (_size == sizeof(Int64))
  17.             {
  18.                 Marshal.WriteInt64(ptr, val);
  19.             }
  20.         }
  21.  
  22.         public IntPtr Ptr {
  23.             get { return ptr; }
  24.         }
  25.  
  26.         ~DisposableIntPtr() {
  27.             Dispose(false);
  28.         }
  29.  
  30.         public void Dispose() {
  31.             Dispose(true);
  32.             GC.SuppressFinalize(this);
  33.         }
  34.  
  35.         protected virtual void Dispose(bool disposing) {
  36.             if (ptr != IntPtr.Zero) {
  37.                 Marshal.FreeHGlobal(ptr);
  38.                 ptr = IntPtr.Zero;
  39.             }
  40.         }
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement