29
votes

I'm experiencing crashes of an app that uses UIWebView. Usually it's when page is not fully loaded and UIWebView is sent stopLoading selector. Or when UIWebView fully loaded page. I've got EXC_BAD_ACCESS. Stack looks like this:

#0  0x95bb7688 in objc_msgSend
#1  0x30a671db in -[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:]
#2  0x3024a10d in __invoking___
#3  0x30249ff8 in -[NSInvocation invoke]
#4  0x358ab160 in HandleDelegateSource
#5  0x302452c1 in CFRunLoopRunSpecific
#6  0x30244628 in CFRunLoopRunInMode
#7  0x32044c31 in GSEventRunModal
#8  0x32044cf6 in GSEventRun
#9  0x309021ee in UIApplicationMain
#10 0x0000239c in main at main.m:13

for me most strange thing here is webView:decidePolicyForNavigationAction:request:frame:decisionListener: selector sent to UIWebView, because there is no such selector in UIWebView documentation! Only for Cocoa (not cocoa touch) WebView. I suspect that there is something wrong with UIWebView or its delegate. But I can't set breakpoint to watch them. Please advise how I can get more info in this situation.

6
What about the [UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] ??Ahsan

6 Answers

87
votes

You have to stop loading the webView and remove the delegate before leaving the view:

// ARC (correct solution)
- (void)dealloc {
    [_webView setDelegate:nil];
    [_webView stopLoading];
}

// non ARC
- (void)dealloc {
    [webView setDelegate:nil];
    [webView stopLoading];
    [webView release];
    [super dealloc];
}

// ARC (older solution)
- (void)viewWillUnload {
    [webView setDelegate:nil];
    [webView stopLoading];
}

What Apple documentation is saying: Important Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil. This can be done, for example, in your dealloc method.

9
votes

Try turning on NSZombie and see if something is being released too soon.

It may be that you are canceling the loading of the view and then immediately tearing down your view hierarchy, causing something to be released before the UIWebView is done messing with it.

In this case, the backtrace looks distinctly like it is a delegate that is being released early. Delegate relationships are typically weak and, lacking GC, are a wonderful source for dangling references that causes crashes that look just like this.

5
votes

View Will Disappear is an working option to the accepted answer:

// ARC
- (void)viewWillDisappear:(BOOL)animated{
    [self.webView setDelegate:nil];
    [self.webView stopLoading];
}

This works for iOS 6 perfectly.

2
votes

I was having an EXC_BAD_ACCESS crash on a scrolling UIWebView, but only on the iPad, and only when the user had left the UIWebView scrolling when s/he closed the view controller containing it.

Setting the delegate to nil didn't fix my problem here, but I found the solution elsewhere under a different issue. I added this code to the method called by my close button:

for (id subview in webView.subviews){
        if ([[subview class] isSubclassOfClass: [UIScrollView class]]){
            [subview setContentOffset:CGPointZero animated:NO];
        }
    }

This stops the scrolling before the dealloc method gets called, which seems to be the issue.

1
votes

I also saw this exact error, and it was caused by the delegate I had designated to a UIWebView not being retained (in my case a UIViewController).

0
votes

Handling unregistering the delegate from the webview, and stopping the webview from loading,is best handled from the ViewController's dealloc method.

As an example of when viewWillDisappear can fail: if the ViewController is a child of another ViewController, you can trigger the removal of the ViewController's view from the parent ViewController's view with an animation. At the same time, you can remove the ViewController from its parent and nil out its reference. At that point the ViewController will be nil and viewWillDisappear will never be called, meaning the WebView delegate will never be cleaned up.

Use dealloc and ensure that your WebView is always cleaned up.