示例#1
0
    public static int Main()
    {
        int[] arr = new int[1000];
        GCHandle[] arrhandle = new GCHandle[10000]; // array of handles to the same object
        IntPtr[] oldaddress = new IntPtr[10000];        // store old addresses
        IntPtr[] newaddress = new IntPtr[10000];        // store new addresses

        for (int i = 0; i < 10000; i++)
        {
            arrhandle[i] = GCUtil.Alloc(arr, GCHandleType.Pinned);
            oldaddress[i] = GCUtil.AddrOfPinnedObject(arrhandle[i]);
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();

        for (int i = 0; i < 10000; i++)
        {
            newaddress[i] = GCUtil.AddrOfPinnedObject(arrhandle[i]);
        }

        for (int i = 0; i < 10000; i++)
        {
            if (oldaddress[i] != newaddress[i])
            {
                Console.WriteLine("Test failed");
                return 1;
            }
        }

        Console.WriteLine("Test passed");
        return 100;
    }
示例#2
0
    public static int Main()
    {
        float[]  arr     = new float[100];
        GCHandle handle1 = GCUtil.Alloc(arr, GCHandleType.Pinned);
        GCHandle handle2 = GCUtil.Alloc(arr, GCHandleType.Normal);

        IntPtr oldaddr, newaddr;

        oldaddr = GCUtil.AddrOfPinnedObject(handle1);
        Console.WriteLine("Address of obj: {0}", oldaddr);

        GC.Collect();
        GC.WaitForPendingFinalizers();

        //			handle1.Free();		// arr should only have normal handle now
        GCUtil.Free(ref handle1);
        GC.Collect();
        GC.WaitForPendingFinalizers();

        GC.Collect();
        GC.WaitForPendingFinalizers();

        try
        {
            Console.WriteLine("Address of obj: {0}", handle1.AddrOfPinnedObject());
        }
        catch (Exception e)
        {
            Console.WriteLine("Caught: " + e);
        }

        arr = null;
        GC.Collect();

        // Pinning the arr again..it should have moved
        GCHandle handle3 = GCUtil.Alloc(arr, GCHandleType.Pinned);

        newaddr = GCUtil.AddrOfPinnedObject(handle3);

        Console.WriteLine("Address of obj: {0}", newaddr);

        if (oldaddr == newaddr)
        {
            Console.WriteLine("Test failed!");
            return(1);
        }
        else
        {
            Console.WriteLine("Test passed!");
            return(100);
        }
    }
示例#3
0
    public static int Main()
    {
        int NUM = 2500;

        int[][]    arr    = new int[NUM][];
        GCHandle[] handle = new GCHandle[NUM];

        IntPtr[] oldaddr = new IntPtr[NUM];

        for (int i = 0; i < NUM; i++)
        {
            arr[i]     = new int[NUM];
            handle[i]  = GCUtil.Alloc(arr[i], GCHandleType.Pinned);
            oldaddr[i] = GCUtil.AddrOfPinnedObject(handle[i]);
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();

        for (int i = 0; i < NUM; i++)
        {
            if (GCUtil.AddrOfPinnedObject(handle[i]) != oldaddr[i])
            {
                Console.WriteLine("Test failed!");
                return(1);
            }
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();

        for (int i = 0; i < NUM; i++)
        {
            if (handle[i].IsAllocated != true)
            {
                Console.WriteLine("Test failed!");
                return(1);
            }
        }

        Console.WriteLine("Test passed!");
        return(100);
    }
示例#4
0
    public static int Main()
    {
        int[]    arr    = new int[100];
        GCHandle handle = GCUtil.Alloc(arr, GCHandleType.Pinned);
        GCHandle hhnd   = GCUtil.Alloc(handle, GCHandleType.Normal);

        GC.Collect();
        GC.WaitForPendingFinalizers();

        Console.WriteLine("Address of obj: {0}", GCUtil.AddrOfPinnedObject(handle));
        try
        {
            Console.WriteLine("Address of handle {0}", GCUtil.AddrOfPinnedObject(hhnd));
        }
        catch (Exception e)
        {
            Console.WriteLine("Caught: {0}", e.ToString());
            Console.WriteLine("Test passed");
            return(100);
        }

        return(1);
    }