UIWebView EXC_BAD_ACCESS crash

我们两清 提交于 2019-11-28 16:51:59

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.

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.

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.

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.

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).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!