Question

I'm trying to load a webpage in a WebView in order to take a snapshot of the website. The WebView is contained in a temporary window that I create for this purpose. However, shortly after I release the WebView and the temporary window, the WebView is sent another release message, while it has already been deallocated. This is the error message in the debugger with NSZombieEnabled set to YES.

*** -[WebView release]: message sent to deallocated instance 0x608000125820

I can't figure out what is causing the WebView to be released too many times. The thing that makes it extra confusing is that the problem only occurs while loading certain URL's. For example: when trying to take a snapshot of http://www.google.com everything is fine, but when using http://edition.cnn.com it almost always crashes.

This is what (a simplified version of) the code looks like:

@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;
}
Was it helpful?

Solution

When using ARC there seems to be problem with retaining/releasing the WebView in some situations. From my testings I found out that loading an empty NSStringin the mainFrame of the WebView before releasing it should solve the problem.

See also a short blog entry on this topic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top