Example #1
0
    public void RefCount2Test()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        // No copy, new, or alloc so ref count is one and it's owned by the pool.
        PrettyData direct = PrettyData.makeDefault();

        Assert.AreEqual(1L, direct.retainCount());

        // Alloc so pool has no ownership stake.
        NSObject indirect = (NSObject) new Class("PrettyData").Call("alloc").Call("init");

        Assert.AreEqual(1L, indirect.retainCount());

        // If we send a message to an object its retain count doesn't change.
        int value = (int)direct.Call("get33");

        Assert.AreEqual(33, value);
        Assert.AreEqual(1L, direct.retainCount());

        pool.release();

        // Verify our counts after we empty the release pool.
        Assert.IsTrue(direct.IsDeallocated());
        Assert.AreEqual(1L, indirect.retainCount());
    }
Example #2
0
    public void IVarTest()
    {
        NSObject instance = (NSObject) new Class("Subclass1").Call("alloc").Call("init");

        // ivars start out null
        NSObject data = instance["myData"];

        Assert.IsTrue(data == null);

        // ivars can be set
        Class    klass = new Class("NSString");
        NSObject str   = (NSObject)klass.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello"));
        long     count = str.retainCount();

        instance["myData"] = str;

        // the ref count of the value should be incremented when it is
        // assigned to an ivar
        Assert.AreEqual(count + 1, str.retainCount());

        // and we can get the value we set
        NSObject result = instance["myData"];

        Assert.AreEqual((IntPtr)str, (IntPtr)result);
    }
Example #3
0
    public void RefCount3Test()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        Class    klass     = new Class("NSHashTable");
        NSObject instance1 = (NSObject)klass.Call("alloc").Call("init");

        Assert.AreEqual(1L, instance1.retainCount());

        NSObject instance2 = (NSObject) new Class("NSHashTable").Call("alloc").Call("init");

        Assert.AreEqual(1L, instance2.retainCount());

        pool.release();

        Assert.AreEqual(1L, instance1.retainCount());
        Assert.AreEqual(1L, instance2.retainCount());
    }
Example #4
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());
    }