Objective-C & EXC_BAD_ACCESS
I'm still really new to Objective-C and Cocoa but I am trying to learn. I am creating a simple ToDo manager but I keep getting a EXC_BAD_ACCESS crash and I'm not sure why. The crash is happening in my main.m file here "return NSApplicationMain(argc, (const char **)argv);" so it is really hard to debug.
Here is my actual implementation file for my app delegate.
#import "ToDoAppDelegate.h"
#import "Task.h"
@implementation ToDoAppDelegate
@synthesize textTaskName;
@synthesize taskDate;
@synthesize window;
@synthesize newTaskWindow;
@synthesize tableView;
@synthesize arrayController;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotific开发者_如何学Goation
{
taskArray = [[NSMutableArray alloc] init];
[taskArray retain];
}
- (IBAction)addTaskClick:(id)sender
{
[NSApp beginSheet:newTaskWindow modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:NULL];
[taskDate setDateValue:[NSDate date]];
}
- (IBAction)btnSaveClick:(id)sender
{
Task *newTask = [[Task alloc] init];
[newTask setTaskName:[textTaskName stringValue]];
[newTask setTaskDueDate:[taskDate dateValue]];
[arrayController addObject:newTask];
[newTask release];
[textTaskName setStringValue:@""];
[NSApp endSheet:newTaskWindow];
[newTaskWindow orderOut:self];
}
- (IBAction)btnCancelClick:(id)sender
{
[NSApp endSheet:newTaskWindow];
[newTaskWindow orderOut:self];
}
@end
What happens is when the btnSaveClick method is called, I get the EXC_BAD_ACCESS crash right after the method finishes executing.
Here is the backtrace for the crash:
(gdb) bt
#0 0x00007fff851d212d in objc_msgSend ()
#1 0x00007fff80f9d1e6 in _CFAutoreleasePoolPop ()
#2 0x00007fff809a0fe0 in -[NSAutoreleasePool drain] ()
#3 0x00007fff8780451f in -[NSApplication run] ()
#4 0x00007fff877fd1a8 in NSApplicationMain ()
#5 0x0000000100001a82 in main (argc=1, argv=0x7fff5fbff638) at /Users/mattwise1985/Documents/Development/xCode Projects/ToDo/ToDo/main.m:13
Since this is just a test project I don't mind if someone wants to download it to check out what I have wrong. It can be downloaded from here: http://www.narfsoft.com/downloads/ToDo.zip
Try enabling NSZombie. Most likely you are sending a message to an object after it has been released.
http://www.cocoadev.com/index.pl?NSZombieEnabled
Are you sure you set the outlet for arrayController
, and that it's not nil
?
By the way:
taskArray = [[NSMutableArray alloc] init];
[taskArray retain];
You'll need to release twice you array, as you retain it after allocating it explicitly...
As Jonathan said, a backtrace from GDB would be appreciated...
This problem happens because you’ve set the bindings for the table view columns but you haven’t set the bindings for the table view itself.
In MainMenu.xib, select the table view and bind its contents (Table Content > Content) to the array controller, key arrangedObjects
. While you’re at it, bind its selection indexes (Table Content > Selection Indexes) to the array controller, key selectionIndexes
.
精彩评论