更新 |我已经使用面板上传了一个示例项目并在此处崩溃:http://w3style.co.uk/~d11wtq/BlocksCrash.tar.gz(我知道“选择...”按钮没有任何作用,我还没有实现它)。
更新2 |刚刚发现我什至不需要调用任何东西newFilePanel
为了导致崩溃,我只需要在声明中使用它。
这也会导致崩溃:
[newFilePanel beginSheetModalForWindow:[windowController window] completionHandler:^(NSInteger result) {
newFilePanel; // Do nothing, just use the variable in an expression
}];
似乎转储到控制台的最后一个内容有时是这样的:“无法反汇编 dyld_stub_objc_msgSend_stret。”,有时是这样的:“无法访问地址 0xa 处的内存”。
我创建了自己的工作表(NSPanel 子类),它尝试提供类似于 NSOpenPanel/NSSavePanel 的 API,因为它将自身呈现为工作表并在完成后调用块。
这是界面:
//
// EDNewFilePanel.h
// MojiBaker
//
// Created by Chris Corbyn on 29/12/10.
// Copyright 2010 Chris Corbyn. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@class EDNewFilePanel;
@interface EDNewFilePanel : NSPanel <NSTextFieldDelegate> {
BOOL allowsRelativePaths;
NSTextField *filenameInput;
NSButton *relativePathSwitch;
NSTextField *localPathLabel;
NSTextField *localPathInput;
NSButton *chooseButton;
NSButton *createButton;
NSButton *cancelButton;
}
@property (nonatomic) BOOL allowsRelativePaths;
+(EDNewFilePanel *)newFilePanel;
-(void)beginSheetModalForWindow:(NSWindow *)aWindow completionHandler:(void (^)(NSInteger result))handler;
-(void)setFileName:(NSString *)fileName;
-(NSString *)fileName;
-(void)setLocalPath:(NSString *)localPath;
-(NSString *)localPath;
-(BOOL)isRelative;
@end
以及实现中的关键方法:
-(void)beginSheetModalForWindow:(NSWindow *)aWindow completionHandler:(void (^)(NSInteger result))handler {
[NSApp beginSheet:self
modalForWindow:aWindow
modalDelegate:self
didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:(void *)[handler retain]];
}
-(void)dismissSheet:(id)sender {
[NSApp endSheet:self returnCode:([sender tag] == 1) ? NSOKButton : NSCancelButton];
}
-(void)sheetDidEnd:(NSWindow *)aSheet returnCode:(NSInteger)result contextInfo:(void *)contextInfo {
((void (^)(NSUInteger result))contextInfo)(result);
[self orderOut:self];
[(void (^)(NSUInteger result))contextInfo release];
}
如果我的块只是一个空体的无操作,那么这一切都有效。当工作表被解除时,我的块被调用。
EDNewFilePanel *newFilePanel = [EDNewFilePanel newFilePanel];
[newFilePanel setAllowsRelativePaths:[self hasSelectedItems]];
[newFilePanel setLocalPath:@"~/"];
[newFilePanel beginSheetModalForWindow:[windowController window] completionHandler:^(NSInteger result) {
NSLog(@"I got invoked!");
}];
但是,当我尝试从块内部访问面板时,我会因 EXC_BAD_ACCESS 崩溃。例如,这会崩溃:
EDNewFilePanel *newFilePanel = [EDNewFilePanel newFilePanel];
[newFilePanel setAllowsRelativePaths:[self hasSelectedItems]];
[newFilePanel setLocalPath:@"~/"];
[newFilePanel beginSheetModalForWindow:[windowController window] completionHandler:^(NSInteger result) {
NSLog(@"I got invoked and the panel is %@!", newFilePanel);
}];
调试器不清楚原因是什么。堆栈上的第一项(零 0)只是显示“??”并且没有列出任何内容。
堆栈中的下一个项目(1 和 2)是对-endSheet:returnCode:
and -dismissSheet:
分别。查看调试器中的变量,似乎没有任何错误/超出范围。
我原以为面板可能已被释放(因为它是自动释放的),但甚至调用-retain
创建后立即使用它并没有帮助。
我执行这个错误吗?