Objective c 带有beginSheetModalForWindow的NSAlert错误
ABTRACT: 当方法Objective c 带有beginSheetModalForWindow的NSAlert错误,objective-c,Objective C,ABTRACT: 当方法beginSheetModalForWindow:modalDelegate:DiEndSelector:contextInfo为contextInfo提供一个对象,该对象是NSArray或NSDictionary时,实际上它将成为指向内存中任何位置的任意指针。解决这个问题的正确方法是什么 以下说明了问题: - (IBAction)testbutton:(id)sender { NSAlert *alert = [[NSAlert alloc] init];
beginSheetModalForWindow:modalDelegate:DiEndSelector:contextInfo
为contextInfo提供一个对象,该对象是NSArray或NSDictionary时,实际上它将成为指向内存中任何位置的任意指针。解决这个问题的正确方法是什么
以下说明了问题:
- (IBAction)testbutton:(id)sender {
NSAlert *alert = [[NSAlert alloc] init];
[alert beginSheetModalForWindow:theWindow
modalDelegate:self
idEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:[NSArray arrayWithObject:@"bla bla"]];
}
}
NSLog将输出内存中的任意内容,包括指针为空时的崩溃。
但是,如果contextInfo是更简单的东西,例如NSString、NSNumber,甚至是NSTextField之类的出口,那么它就可以正常工作。另外,如果我提供的实例变量是NSArray或NSDictionary,那么它就可以工作
在Apple文档中,contextInfo声明为(void*),因此它可以是任何东西(甚至是任意指针)。但是因为我知道这个类,所以在这个例子中我声明它为NSArray。然而,问题并不取决于我声明的contextInfo是什么
当我进行垃圾收集时,问题发生在Lion中
在消息“noa”中说,“如果您使用垃圾收集,请将其存储在实例变量中–无论在何处,都无所谓–因此它不会在运行循环结束时释放。您需要一直等待,直到DiEndSelector可以对其进行操作。”
这是一个很好的解决办法,但它很奇怪,而且相当丑陋。难道没有更优雅的解决方案吗
这种行为真的是苹果想要的吗?还是一只清晰的虫子?NSString和NSNumber等简单对象可以,但不是NSArray。如果它真的不是一个bug,那么是否应该指定contextInfo的限制,是否应该确切地指定它需要以某种方式显式保留的时间和方式?或者,除非contextInfo是一个实例变量,否则GC就不会用这个方法保证任何东西吗
- (void) alertDidEnd:(NSAlert *)alert
returnCode:(NSInteger)returnCode
contextInfo:(NSArray *)contextInfo {
NSLog(@"contextInfo: %@", contextInfo);