I’m trying to develop a simple application, which upon clicking a menu item, it shows a window containing an NSTableView.
The problem is that the app crashes just after NSTableView displaying the data. Full stack trace:
* thread #1: tid = 0x2107, 0x00007fff943bce90 libobjc.A.dylib\`objc_msgSend + 16,
stop reason = EXC_BAD_ACCESS (code=13, address=0x0)
frame #0: 0x00007fff943bce90 libobjc.A.dylib`objc_msgSend + 16
Since I’m using ARC, I should exclude any reference counting issue; but maybe I’m creating the controller (needed to create the window) in a bad way, and it’s being free’d erroneously.
This is the code of the AppController that create and shows the window:
- (IBAction)showPreferences:(id)sender {
if(!preferencesWindow) {
preferencesWindow = [[[PreferencesWindowController alloc]
initWithWindowNibName:@"PreferencesWindow"] window];
}
[preferencesWindow makeKeyAndOrderFront:sender];
}
This code in PreferencesWindowController implements the dataSource protocol (needed by the NSTableView).
- (int)numberOfRowsInTableView:(NSTableView *)tabView {
return 1;
}
- (id)tableView:(NSTableView *)tabView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
NSString *val = [NSString stringWithFormat:@"%@[%d]", [tableColumn identifier], row];
return val;
}
It’s not causing the crash per se. But if I remove the PreferencesWindowController from NSTableView’s dataSource, it doesn’t crash, so it should be somewhat related.
Where’s the mistake?
EDIT: using the profiler (Instruments) with the zombies preset, I can see there’s an object whose reference counter goes negative:

but anyway, the stack is outside the code I wrote. I can’t put a breakpoint there, and I can’t see which is the object being released twice (or I should say I don’t know how to)
The line
preferencesWindow = [[[PreferencesWindowController alloc] initWithWindowNibName:@"PreferencesWindow"] window]looks suspicious, because while you are referencing the window itself with a strong reference, it looks like you’re letting ARC release thePreferencesWindowController.Try storing the
PreferencesWindowControllerobject in its ownstrongvariable/property and let me know.