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()); }
public void IntArg() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); long bytes = DoGetMemory(); Class nsString = new Class("NSString"); NSObject str = (NSObject)nsString.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello world")); for (int j = 1; j < 100; ++j) { for (int i = 0; i < NumIterations / 100; ++i) { str.Call("characterAtIndex:", 2); } GC.Collect(); } pool.release(); GC.Collect(); GC.WaitForPendingFinalizers(); long delta = DoGetMemory() - bytes; if (delta / NumIterations > 4) { Assert.Fail("IntArg used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations); } }
public void NilCallTest() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); NSObject nil = new NSObject(IntPtr.Zero); // Calling an NSObject method on nil does nothing and returns nil. NSObject result = (NSObject)nil.Call("hash"); Assert.IsTrue(result.IsNil()); // Calling a unknown method on nil does nothing and returns nil. result = (NSObject)nil.Call("foo"); Assert.IsTrue(result.IsNil()); // Can chain calls to nil. result = (NSObject)nil.Call("foo").Call("bar"); Assert.IsTrue(result.IsNil()); // Can use Native with null. result = (NSObject)nil.Call("foo"); Assert.IsTrue(result.IsNil()); pool.release(); }
public void Released() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); long bytes = DoGetMemory(); for (int j = 1; j < 100; ++j) { for (int i = 0; i < NumIterations / 100; ++i) { Class klass = new Class("NSNumber"); klass.release(); } GC.Collect(); } pool.release(); GC.Collect(); GC.WaitForPendingFinalizers(); long delta = DoGetMemory() - bytes; if (delta / NumIterations > 4) { Assert.Fail("Released used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations); } }
public void Managed() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); long bytes = DoGetMemory(); for (int j = 1; j < 100; ++j) { for (int i = 0; i < NumIterations / 100; ++i) { NSObject instance = (NSObject) new Class("Subclass1").Call("alloc").Call("init"); instance.Call("TakeString", "what"); instance.release(); } GC.Collect(); } pool.release(); GC.Collect(); GC.WaitForPendingFinalizers(); long delta = DoGetMemory() - bytes; if (delta / NumIterations > 4) { Assert.Fail("Managed used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations); } }
public void Formatted() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); long bytes = DoGetMemory(); for (int j = 1; j < 100; ++j) { for (int i = 0; i < NumIterations / 100; ++i) { NSObject s = (NSObject)Native.Call("[[NSNumber alloc] initWithInteger:{0}]", 33); s.release(); } GC.Collect(); } pool.release(); GC.Collect(); GC.WaitForPendingFinalizers(); long delta = DoGetMemory() - bytes; if (delta / NumIterations > 4) { Assert.Fail("Formatted used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations); } }
public void ArrayArg() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); Class nsData = new Class("NSData"); long bytes = DoGetMemory(); for (int j = 1; j < 100; ++j) { for (int i = 0; i < NumIterations / 100; ++i) { byte[] data = new byte[] { 2, 5, 6, 3 }; NSObject d = new NSObject(nsData.Call("alloc")); NSObject e = (NSObject)d.Call("initWithBytes:length:", data, data.Length); e.release(); } GC.Collect(); } pool.release(); GC.Collect(); GC.WaitForPendingFinalizers(); long delta = DoGetMemory() - bytes; if (delta / NumIterations > 4) { Assert.Fail("ArrayArg used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations); } }
// Action methods to add/remove boxes, giving us something to animate. Note that we // cause a relayout here; a better design is to relayout in the view automatically on // addition/removal of subviews. public void addABox(NSObject sender) { NSObject box = DoNewBox(); AddSubView(box); box.release(); DoLayout(); }
public void ThreadedTest() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); sbyte threaded = (sbyte)new Class("NSThread").Call("isMultiThreaded"); Assert.AreEqual(1, threaded); pool.release(); }
public void DeallocTest2() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); MyDerived instance = (MyDerived) new Class("MyDerived").Call("alloc").Call("init"); Assert.AreEqual(1L, instance.retainCount()); instance.release(); Assert.IsTrue(instance.Dead); pool.release(); }
public void ChainedCallTest() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); Class nsString = new Class("NSMutableString"); NSObject str = (NSObject)nsString.Call("alloc").Call("initWithUTF8String:", Marshal.StringToHGlobalAuto("chained!")); string result = Marshal.PtrToStringAuto((IntPtr)str.Call("UTF8String")); Assert.AreEqual("chained!", result); pool.release(); }
public void SuperTest() { NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); Media instance = (Media) new Class("Media").Call("alloc").Call("init"); int value = instance.value(); Assert.AreEqual(3, value); value = instance.Call("value").To <int>(); Assert.AreEqual(3, value); pool.release(); }
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()); }
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()); }
private void DoMemoryThread(object arg) { NSObject view = (NSObject)arg; TimeSpan interval = TimeSpan.FromSeconds(5); Random rand = new Random(1); NSObject pool = (NSObject) new Class("NSAutoreleasePool").Call("alloc").Call("init"); lock (m_lock) { int count = 0; // note that this isn't the count of boxes: it's the count of boxes added by this thread while (m_checkingMemory) { bool signaled = Monitor.Wait(m_lock, interval); if (!signaled) { Selector selector; if ((rand.Next(2) == 0 || count == 0) && count < 12) { selector = new Selector("addABox:"); ++count; } else { selector = new Selector("removeLastBox:"); --count; } view.Call("performSelectorOnMainThread:withObject:waitUntilDone:", selector, null, false); } } } pool.release(); }
internal static void Main(string[] args) { try { Registrar.CanInit = true; // Make our app a foreground app (this is redundant if we were started via the // Finder or the open command, but important if we were started by directly // executing the launcher script). var psn = new ProcessSerialNumber(); psn.highLongOfPSN = 0; psn.lowLongOfPSN = kCurrentProcess; int err = TransformProcessType(ref psn, kProcessTransformToForegroundApplication); if (err != 0) { throw new InvalidOperationException("TransformProcessType returned " + err + "."); } err = SetFrontProcess(ref psn); if (err != 0) { throw new InvalidOperationException("SetFrontProcess returned " + err + "."); } // Load the nib and run the main event loop. NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); App app = new App("MainMenu.nib"); pool.release(); app.Run(); } catch (Exception e) { Console.WriteLine(e); } }