尝试打印时EXC_BAD_ACCESS

时间:2012-04-10 15:40:34

标签: objective-c cocoa printing nsprintoperation

所以,我正在尝试测试打印功能,并且我不断收到EXC_BAD_ACCESS错误,指出这部分代码:

[op runOperationModalForWindow:(NSWindow*)[core window]
                      delegate:self
                didRunSelector:
 @selector(printOperationDidRun:success:contextInfo:)
                   contextInfo:NULL];

以上代码完全取决于Apple的开发人员文档。

我在此代码块之前放置了一个断点,似乎[core window]绝对不是NULL并已分配。

那么,怎么了?有什么想法吗?


更新:

试过这个:

[self setPrintView:[[[NSTextView alloc] initWithFrame:NSMakeRect(0,0,200,200)]
  retain]];

[[self printView] setString:[[[[core editor] currentDocument] editorView] string]];
[[self printView] setHidden:NO];

NSPrintOperation *op = [NSPrintOperation
                        printOperationWithView:[[self printView] retain]
                        printInfo:nil];

[op setCanSpawnSeparateThread:YES];
[op setShowsPrintPanel:NO];
[op runOperation];

我现在得到了:

*** -[NSStorage insertElement:atIndex:]: message sent to deallocated instance 0x121267ff0

1 个答案:

答案 0 :(得分:0)

我刚发现:

诀窍是在打印功能结束时release我的NSTextView

       ...
       [printView release];
}