gpt4 book ai didi

macos - EXC_BAD_ACCESS 当我关闭窗口时,这也是我的应用程序的委托(delegate)

转载 作者:行者123 更新时间:2023-12-03 17:28:37 24 4
gpt4 key购买 nike

我编写了一个 Cocoa 应用程序,当我关闭应用程序窗口时出现 EXC_BAD_ACCESS 错误。我读到此错误通常意味着内存问题,但我打开了 ARC 模式,并且不需要关心释放等问题。 (xCode 禁止我调用此函数并自动管理内存)。

错误指向主函数中的行 return NSApplicationMain(argc, (const char **)argv);

这是我的应用程序的代码:

.h 文件:

@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 文件:

@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

谢谢

更新

我使用个人资料来查找僵尸事件。所以我发现了这个:一条 Objective-C 消息被发送到一个已释放的对象(zombie(位于地址 0x108d85230)

负责任的调用者 - [NSApplication(NSWindowCache) _checkForTerminateAfterLastWindowClosed: saveWindows:]

我在代码中有这个函数:

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
return TRUE;
}

但是我把它放在评论中后,这个僵尸事件仍然发生。

最佳答案

崩溃是由于您将窗口设为应用程序的委托(delegate)而引起的。当您关闭窗口时,这是杀死它的最后一个版本,如果这是您打开的最后一个窗口,它会导致应用程序询问其委托(delegate)是否应该退出。由于您刚刚杀死的窗口是应用程序的委托(delegate),因此您会发生崩溃。

Longer explanation and suggestion of solution in my answer on your subsequent question.

关于macos - EXC_BAD_ACCESS 当我关闭窗口时,这也是我的应用程序的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com