Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios:EXC_BAD_ACCESS for Webview delegate

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?

like image 670
Ankit Sachan Avatar asked Mar 19 '15 07:03

Ankit Sachan


2 Answers

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

like image 97
Durai Amuthan.H Avatar answered Sep 19 '22 00:09

Durai Amuthan.H


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

like image 45
vk.edward.li Avatar answered Sep 22 '22 00:09

vk.edward.li