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); } }
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); }
internal static void Init() { if (!ms_inited) { if (!ms_canInit) { throw new InvalidOperationException("mobjc was used, but CanInit is false"); } // This will force AppKit and Foundation to load if they have not already been loaded // (normally the static NSApplication ctor in mcocoa will take care of this, but some // apps may want to create NSObjects before that). Unused.Value = NSAvailableWindowDepths(); NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool")); DoInit(); // NSAutoreleasePool cannot be used within Posix threads unless Cocoa // is switched to "multithreading mode". We always have at least two // threads (the main thread and the finalizer thread) so we'll switch // to multithreading mode here. Selector selector = new Selector("foo"); NSObject thread = new Class("NSThread").Call("alloc").Call("initWithTarget:selector:object:", null, selector, null).To <NSObject>(); Unused.Value = thread.Call("start"); ms_inited = true; 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(); }
/// <summary> /// Used for regression tests. /// </summary> public static void test_regression() { Registrar.CanInit = true; Managed.LogException = delegate { }; NSObject pool = null; var test = new MobjcTest(); test.OnBeginTest += () => { pool = (NSObject) new Class("NSAutoreleasePool").Call("alloc").Call("init"); }; test.OnEndTest += () => { pool.release(); }; test.TestAll(typeof(NSObject).Assembly); }
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 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 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 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 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 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 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()); }
public void Native() { NSObject pool = new NSObject(NSObject.CreateNative("NSAutoreleasePool")); Random rng = new Random(1); NSMutableArray values = NSMutableArray.Create(); for (int i = 0; i < ArraySize; ++i) values.addObject(NSString.Create(rng.Next().ToString())); Stopwatch timer = Stopwatch.StartNew(); for (uint i = 0; i < values.count(); ++i) { for (uint j = values.count() - 1; j > i; --j) { NSString s1 = values.objectAtIndex(j).To<NSString>(); NSString s2 = values.objectAtIndex(j - 1).To<NSString>(); if (s2.compare(s1) > 0) { values.replaceObjectAtIndexWithObject(j, s2); values.replaceObjectAtIndexWithObject(j - 1, s1); } } } Console.WriteLine("native {0:0.0} secs", timer.ElapsedMilliseconds/1000.0); for (uint i = 0; i < Math.Min(values.count() - 1, 100); ++i) { NSString s1 = values.objectAtIndex(i).To<NSString>(); NSString s2 = values.objectAtIndex(i + 1).To<NSString>(); Assert.IsTrue(s1.compare(s2) <= 0); } pool.release(); }