Search code examples
c#idisposableintptr

Proper IntPtr use in C#


I think I understand the use of IntPtr, though I'm really not sure.

I copied the IDisposable pattern from MSDN just to see what I could get from it, and while I understand it for the most part, I have no idea how to implement an IntPtr properly, or even understand what it is that it's supposed to "point" to, or reference. On top of that, I have no idea how to even assign or cast an integer, string, char, double, etc. to an IntPtr to create a pointer out of it.

Also, does IntPtr require unsafe code use?

Anyway, here's some code just to paint a picture of what I'm talking about:

namespace Utilities
{   
    class Disposer : IDisposable
    {

        private IntPtr handle;

        private Component component = new Component(); 

        private bool disposed = false;

        public Disposer(IntPtr handle)
        {
            this.handle = handle;

        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if (disposing)
                {
                    component.Dispose(); 
                }
                CloseHandle(handle);

                handle = IntPtr.Zero;

                disposed = true;

            }
        }

        [System.Runtime.InteropServices.DllImport("Kernal32")]
        private extern static Boolean CloseHandle(IntPtr handle);
    }



    public unsafe class ExecuteMain
    {
        Object nuller = new Object();

        byte boa = 0;

        byte *blargh = boa;

        public static void Main()
        { 

        }
    }
}

Also, could someone tell me what the point of the component here is, exactly? I'm also having trouble wrapping my head around this concept as well.


Solution

  • You can use IntPtr objects this way:

    int test = 55;
    
    // Allocating memory for int
    IntPtr intPointer = Marshal.AllocHGlobal(sizeof(int));
    
    Marshal.WriteInt32(intPointer,test);
    
    // sending intPointer to unmanaged code here
    
    //Test reading of IntPtr object
    int test2 = Marshal.ReadInt32(intPointer); // test2 would be equal 55
    
    // Free memory
    Marshal.FreeHGlobal(intPointer);
    

    You can explore other Marshal method to understand how to write string, doubles and etc to IntPtr.

    So words about your sample code - its not a good idea to dispose external allocated unmanaged object. You should dispose only object which you have allocated in class constructor. This is not strict rule but some kind of good practice.