【发布时间】:2014-02-28 14:21:37
【问题描述】:
我正在尝试在 WebView 中加载网页以获取网站的快照。 WebView 包含在我为此目的创建的临时窗口中。但是,在我释放 WebView 和临时窗口后不久,WebView 被发送另一个释放消息,而它已经被释放。这是调试器中的错误消息,其中 NSZombieEnabled 设置为 YES。
*** -[WebView release]: message sent to deallocated instance 0x608000125820
我无法弄清楚是什么导致 WebView 被释放太多次。使它更加混乱的是,问题仅在加载某些 URL 时发生。例如:尝试拍摄http://www.google.com 的快照时一切正常,但使用http://edition.cnn.com 时几乎总是崩溃。
代码如下(简化版):
@interface AppDelegate ()
@property (nonatomic, strong) NSWindow *tempWindow;
@property (nonatomic, strong) WebView *webView;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CGRect frame = CGRectMake(0, 0, 1200, 695);
self.tempWindow = [[NSWindow alloc] initWithContentRect:frame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
self.webView = [[WebView alloc] initWithFrame:frame];
self.webView.frameLoadDelegate = self;
self.tempWindow.contentView = self.webView;
[[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://edition.cnn.com"]]];
}
#pragma mark - WebFrameLoadDelegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if (frame != [sender mainFrame]) {
return;
}
// take snapshot here...
[self takeSnapshot];
// get rid of web view and temp window
[self.webView stopLoading:nil];
[self.webView setFrameLoadDelegate:nil];
self.webView = nil;
self.tempWindow = nil;
}
【问题讨论】:
标签: objective-c macos webkit foundation