ios:EXC_BAD_ACCESS for Webview delegate

2020-02-09 14:39发布

I have a situation where I am trying to resolve these Crashlytics issues and I have this crash log

Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x34217f46 objc_msgSend + 5
1  UIKit                          0x29a2d5a3 -[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] + 182
2  CoreFoundation                 0x2630cad4 __invoking___ + 68
3  CoreFoundation                 0x26239645 -[NSInvocation invoke] + 300
4  CoreFoundation                 0x2623d0c7 -[NSInvocation invokeWithTarget:] + 50
5  WebKitLegacy                   0x326d9261 -[_WebSafeForwarder forwardInvocation:] + 224
6  CoreFoundation                 0x2630b62f ___forwarding___ + 354
7  CoreFoundation                 0x2623d008 _CF_forwarding_prep_0 + 24
8  CoreFoundation                 0x2630cad4 __invoking___ + 68
9  CoreFoundation                 0x26239645 -[NSInvocation invoke] + 300
10 WebCore                        0x31c02729 HandleDelegateSource(void*) + 100
11 CoreFoundation                 0x262cefbf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
12 CoreFoundation                 0x262ce461 __CFRunLoopDoSources0 + 364
13 CoreFoundation                 0x262cca35 __CFRunLoopRun + 772
14 CoreFoundation                 0x2621a3b1 CFRunLoopRunSpecific + 476
15 CoreFoundation                 0x2621a1c3 CFRunLoopRunInMode + 106
16 GraphicsServices               0x2d801201 GSEventRunModal + 136
17 UIKit                          0x2988443d UIApplicationMain + 1440
18 abc                          0x0030dcd7 main (main.m:14)

I can understand that its some callback on webview delegate and bad excess has occurred, so to rectify this I handled the delegates via

[self.webview stopLoading];
self.webview.delegate =nil;

in all the classes, yet I can see this crash. Can you enlighten me what's possibly going wrong and some approach to rectify this?

6条回答
孤傲高冷的网名
2楼-- · 2020-02-09 14:50

Are you adding a web view within your subclass of a web view? Generally that's the problem and if so, changing the superclass to a UIView would solve the problem.

查看更多
走好不送
3楼-- · 2020-02-09 15:06

The following might be the case here

  • The user is presented a screen with UIWebView
  • The UIViewController sets self as the delegate Web page starts downloading
  • The User quits screen
  • UIViewController gets deallocated UIWebView finishes loading and sends I am finished loading message to its delegate

or

some other delegate method gets called when the webview object is no more.i.e dangling pointer effect

1.Always make sure you stop loading the webView and remove the delegate before leaving the view

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, in your dealloc method

Here is the reference

// If ARC is used
- (void)dealloc {
    [_webView setDelegate:nil];
    [_webView stopLoading];
}

// If ARC is not used
- (void)dealloc {
    [webView setDelegate:nil];
    [webView stopLoading];
    [webView release];
    [super dealloc];
}

// ARC - Before iOS6 as its deprecated from it.
- (void)viewWillUnload {
    [webView setDelegate:nil];
    [webView stopLoading];
}

2.Make sure you are not stopLoading and setDelegate to nil in viewWillDisappear

if the ViewController is a child of a another ViewController, u can trigger the removal of the ViewController's view from the parent ViewController's view with an animation. At the same time, u 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.

3.Make sure you set the ContentOffset of the subviews of webview to CGPointZero without animation

In iPad in some versions while webview is scrolling if you close the parent viewcontroller without setting ContentOffset to CGPointZero this kind of problems will come

so its better to you call the following code of in parent viewcontroller before closing it

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

Hope this helps.Feel free to ask your doubts.

4.Generally speaking You should not embed UIWebView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

Here is the reference

查看更多
萌系小妹纸
4楼-- · 2020-02-09 15:07

Try disabling your UIWebView's scrolling behaviour before the ViewController deallocs it

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

p.s. Dipen Chudasama's approach is correct, according to Apple's documentation, you should really set the delegate property to nil before releasing the webview, assuming you have released the webview correctly inside dealloc function but not viewWillDisappear

查看更多
神经病院院长
5楼-- · 2020-02-09 15:11

Use visa versa means first nil your delegate and then after stop loading web view may be help you.

like this.

[_webView setDelegate:nil];
[_webView stopLoading];

As per apple document : Important Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil.

查看更多
太酷不给撩
6楼-- · 2020-02-09 15:12

UIWebViews delegate uses assign and not weak. So you need to nullify the delegate when the webView's controller gets deallocated.

Example:

- (void)dealloc
{
     self.webview.delegate =nil;
}
查看更多
祖国的老花朵
7楼-- · 2020-02-09 15:12

Use [listener use] instead, to tell your UIWebView to handle clicked URL.

-(void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id )listener
{
    [listener use]
}
查看更多
登录 后发表回答