ViewController test works in ASOC but not Obj-C

I asked a question about a simple test app with an NSViewController subclass that just implements one IBAction for a button click on StackOverflow, but wasn’t getting a good response there. So, I rewrote it in ASOC to ask the question here, but it worked in ASOC, so now I’m doubly confused. The app delegates in the 2 apps just have this code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    TestController *controller = [[TestController alloc] initWithNibName:@"TestController" bundle:nil];
    [self.window.contentView addSubview:controller.view];

And in the ASOC version:

on applicationWillFinishLaunching_(aNotification)
		set controller to current application's TestController's alloc()'s initWithNibName_bundle_("TestController", missing value)
		theWindow's contentView()'s addSubview_(controller's view())
	end applicationWillFinishLaunching_

In both versions, I added a new file of the obj-C type with NSViewController as the class – this gives you a xib file with the same name, and in the view provided, I added a button and connected it to the IBAction method I implemented in the view controller’s .m file (it just logs the sender). The button method is connected through the File’s Owner object (which is automatically set to TestController when you create the new file).

The ASOC version works fine, the obj-c version gives various errors on different launches of the same app. I always get either an unrecognized selector error (but sent to different things, like NSArray, NSDictionary, NSSet, or NSRunLoop) or the dreaded EXC_BAD_ACCESS message. How is it possible to get different errors on different runs? And, why doesn’t this work in obj-c? Any ideas?


Memory problems can cause that – if an object has been deallocated, what you get can be anything. What memory management are you using? And which line is raising the error?

I’m using ARC, and I don’t really know what line causes the error, other than it happens when I click the button, and that method only has one line. When I get something other than EXC_BAD_ACCESS, the error log looks like this:

2012-06-02 19:14:19.167 ControllerTests[7865:707] -[NSRunLoop buttonClick:]: unrecognized selector sent to instance 0x10049fed0
2012-06-02 19:14:19.169 ControllerTests[7865:707] -[NSRunLoop buttonClick:]: unrecognized selector sent to instance 0x10049fed0
2012-06-02 19:14:19.174 ControllerTests[7865:707] (
0 CoreFoundation 0x00007fff8c1c8f56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8b617d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8c2551be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8c1b5e23 forwarding + 371
4 CoreFoundation 0x00007fff8c1b5c38 _CF_forwarding_prep_0 + 232
5 CoreFoundation 0x00007fff8c1b870d -[NSObject performSelector:withObject:] + 61
6 AppKit 0x00007fff85094f7e -[NSApplication sendAction:to:from:] + 139
7 AppKit 0x00007fff85094eb2 -[NSControl sendAction:to:] + 88
8 AppKit 0x00007fff85094ddd -[NSCell _sendActionFrom:] + 137
9 AppKit 0x00007fff850942a0 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
10 AppKit 0x00007fff85113fc4 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
11 AppKit 0x00007fff85092eaa -[NSControl mouseDown:] + 786
12 AppKit 0x00007fff8505e348 -[NSWindow sendEvent:] + 6306
13 AppKit 0x00007fff84ff7a55 -[NSApplication sendEvent:] + 5593
14 AppKit 0x00007fff84f8e0c6 -[NSApplication run] + 555
15 AppKit 0x00007fff8520a244 NSApplicationMain + 867
16 ControllerTests 0x0000000100001572 main + 34
17 ControllerTests 0x0000000100001544 start + 52
18 ??? 0x0000000000000003 0x0 + 3

After Edit: I think you nailed it Shane. If I create a property for controller (using retain) instead of just a local variable in the applicationWillFinishLaunching method, then it works fine. I don’t quite understand why it works in ASOC without creating a property – something to do with GC vs. ARC, or something in the bridge that causes controller to be retained in ASOC?