Ejemplo n.º 1
0
    public void RefCount1Test()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        // If we use alloc the object will have a ref count of one.
        NSObject instance = (NSObject) new Class("NSHashTable").Call("alloc").Call("init");
        Assert.AreEqual(1L, instance.retainCount());

        // Classes always have a very high retain count (because they
        // are not supposed to go away).
        Class nsSignature = new Class("NSMethodSignature");
        Assert.IsTrue(nsSignature.retainCount() > 1000);

        // If alloc, new, or copy aren't used then the pool owns the object.
        Class nsString = new Class("NSString");
        NSObject str = (NSObject) nsString.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello"));
        Assert.AreEqual(1L, str.retainCount());

        // We can have two managed instances on the same native instance
        // and the ref count doesn't change.
        NSObject copy = new NSObject((IntPtr) instance);
        Assert.AreEqual(1L, copy.retainCount());

        // If we send a message to an object its retain count doesn't change.
        instance.Call("description");
        Assert.AreEqual(1L, instance.retainCount());

        pool.release();

        // Verify our counts after we empty the release pool.
        Assert.AreEqual(1L, instance.retainCount());
        Assert.AreEqual(1L, copy.retainCount());
    }