Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: WebKitLegacy crashes

Tags:

ios

crash

webkit

After some relatively safe (as for me) modifications the app began crashing on some WebKitLegacy stuff. I see many crashes in Fabric but can't find stable steps to reproduce. Does anyone know what can cause those crashes? Please see images attached.

Some app screens use UIWebView to display content - I assume that the problem is somewhere there.

enter image description here enter image description here

like image 912
Division Avatar asked Feb 16 '16 11:02

Division


2 Answers

UIWebView EXC_BAD_Address ...Application received signal First of All, you should think about the webView.delegate = nil. But where ??

My experience:

- (void)dealloc{
    /*
     Important
     Before releasing an instance of UIWebView for which you have set a delegate,
     you must first set the UIWebView delegate property to nil before disposing of the UIWebView instance. 
     This can be done, for example, in the dealloc method where you dispose of the UIWebView.
     */
    if (_webView.loading) {
        [self.webView stopLoading];
    }
    _webView.delegate = nil;
}

if the ViewController is a child of a 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 this point 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.

Other excellent linking ios:EXC_BAD_ACCESS for Webview delegate

like image 165
Qun Li Avatar answered Oct 31 '22 13:10

Qun Li


As far as I remember, that signal popped up when a Webview received an update (for example, a response from an embedded image), but it couldn't be rendered because it's not used by the main thread anymore. That means, when the ViewController is not being displayed.

If that's the case, you should be able to reproduce the issue by loading a web page with heavy content (for example, some online newspaper like http://edition.cnn.com/) and dismissing the webview immediately after starting the load, by pushing/popping another ViewController.

How to fix it: indeed, you need to call the methods you mentioned:

webview.delegate = nil;
[webview stopLoading];

However, the place to do so is on the viewWillDisappear method, never on dealloc. The reason is simple: viewWillDisappear is called on the exact moment where the ViewController is about to lose control of the main thread. However, dealloc is called when the VC is about to be released to the heap. This might happen some seconds after, giving the app some precious time to crash, or maybe will never be called at all. Moving both methods there should do the trick.

like image 37
Bartserk Avatar answered Oct 31 '22 14:10

Bartserk