Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class DisposableIntPtr : IDisposable {
- IntPtr ptr;
- int _size;
- public DisposableIntPtr(int size) {
- ptr = Marshal.AllocHGlobal(size);
- _size = size;
- }
- public void WriteInt32( int val )
- {
- if (_size == sizeof(Int32))
- {
- Marshal.WriteInt32(ptr, val);
- }
- else if (_size == sizeof(Int64))
- {
- Marshal.WriteInt64(ptr, val);
- }
- }
- public IntPtr Ptr {
- get { return ptr; }
- }
- ~DisposableIntPtr() {
- Dispose(false);
- }
- public void Dispose() {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing) {
- if (ptr != IntPtr.Zero) {
- Marshal.FreeHGlobal(ptr);
- ptr = IntPtr.Zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement