我写了一个 Cocoa 应用程序,然后我得到了EXC_BAD_ACCESS
当我关闭应用程序窗口时出错。我读到这个错误通常意味着内存问题,但我有ARC mode
上,我不需要关心释放等。 (xCode 禁止我调用此函数并自动管理内存)。
错误指向行return NSApplicationMain(argc, (const char **)argv);
在主函数中。
这是我的应用程序的代码:
.h file:
@interface MainDreamer : NSWindow <NSWindowDelegate>
{
NSTextField *dreamField;
NSTableView *dreamTable;
NSImageView *dreamview;
NSMutableArray *dreamlist;
NSMutableArray *dataset;
}
@property (nonatomic, retain) IBOutlet NSTextField *dreamField;
@property (nonatomic, retain) IBOutlet NSTableView *dreamTable;
@property (nonatomic, retain) IBOutlet NSImageView *dreamview;
@property (nonatomic, retain) IBOutlet NSMutableArray *dreamlist;
@property (nonatomic, retain) IBOutlet NSMutableArray *dataset;
@property (assign) IBOutlet NSWindow *window;
@end
.m file:
@implementation MainDreamer
@synthesize window;
@synthesize dataset;
@synthesize dreamField;
@synthesize dreamlist;
@synthesize dreamview;
@synthesize dreamTable;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
NSString *applicationPath = [[NSBundle mainBundle] bundlePath];
NSString *filename = [applicationPath stringByAppendingPathComponent:@"dreams"];
NSLog(self.description);
dreamlist = [[NSMutableArray alloc] init];
dataset = [[NSMutableArray alloc] init];
dataset = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];
if([dataset count] != 0) {
int i = 0;
while (i < [dataset count]) {
Dream *dr = [[Dream alloc] init];
dr = [dataset objectAtIndex:i];
[dreamlist addObject: dr.dreamname];
i++;
}
}
[dreamTable reloadData];
}
-(void)applicationWillTerminate:(NSNotification *)notification{
NSString *applicationPath = [[NSBundle mainBundle] bundlePath];
NSString *filename = [applicationPath stringByAppendingPathComponent:@"dreams"];
[NSKeyedArchiver archiveRootObject:dataset toFile:filename];
NSLog(@"finish");
}
- (void) mouseUp:(NSEvent *)theEvent{
long index = [dreamTable selectedRow];
Dream *dr = [[Dream alloc] init];
dr = [dataset objectAtIndex:index];
dr.dreampicture = dreamview.image;
[dataset replaceObjectAtIndex:index withObject:dr];
NSLog(self.description);
}
- (void) tableViewSelectionDidChange: (NSNotification *) notification{
long row = [dreamTable selectedRow];
Dream *dr = [[Dream alloc] init];
dr = [dataset objectAtIndex: row];
if(dr.dreampicture != NULL)
dreamview.image = dr.dreampicture;
NSLog(@"selected row changed");
}
“梦想”班:
@interface Dream : NSObject <NSCoding>
{
NSString *dreamname;
NSImage *dreampicture;
}
@property (retain) NSString* dreamname;
@property (retain) NSImage* dreampicture;
-(id)initWithCoder:(NSCoder *)aDecoder;
-(void)encodeWithCoder:(NSCoder *)aCoder;
@end
出了什么问题,为什么EXC_BAD_ACCESS
发生了吗?我提醒我有带有自动引用计数(ARC)的 xCode 4
Thanks
UPDATE
我使用 Profile 来查找僵尸事件。所以我发现了这一点:Objective-C 消息被发送到已释放的对象(僵尸(位于地址 0x108d85230)
负责任的来电者 -[NSApplication(NSWindowCache) _checkForTerminateAfterLastWindowClosed: saveWindows:]
我在代码中有这个功能:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
return TRUE;
}
然而在我评论之后,这个僵尸事件却不断发生。