29

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.

Jim Puls
  • 79,175
  • 10
  • 73
  • 78
HARDWARRIOR
  • 841
  • 1
  • 8
  • 22
  • 1
    What about the [UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] ?? – Ahsan Jul 11 '12 at 03:29

6 Answers6

87

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.

Ondrej Rafaj
  • 4,342
  • 8
  • 42
  • 65
  • 3
    In ios 6 use dealloc method with ARC(without calling super or any memory release code) - viewWillUnload is deprecated in ios 6. – Morten Holmgaard Apr 05 '13 at 16:11
  • This does not work in iOS 6, as @MortenHolmgaard pointed out. See my answer below. – jturolla May 20 '13 at 20:15
  • I don't understand why dealloc is a non arc version. In ARC you don't get to call dealloc manually, but it gets called automatically by the system and the code in dealloc gets executed. – The dude Jan 07 '14 at 10:38
  • 1
    Using dealloc should be the standard method for arc and non-arc. in the arc version, omit [webView release]; and [super dealloc];. With the above solution if you present a full screen modal -viewWillDisappear: will get called, and you will have to rewire your webview when -viewWillAppear: is called. – Saltymule Mar 24 '14 at 12:41
  • using [_webView setDelegate:nil]; save my day! I have a viecontrolle that call another one that use a webview, and crash appeared. Now It work! – doxsi Apr 11 '14 at 09:22
  • OP gets downvote (that didn't accept), you get upvote. – l0gg3r Nov 07 '14 at 14:38
9

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.

bbum
  • 162,346
  • 23
  • 271
  • 359
5

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.

jturolla
  • 6,596
  • 7
  • 26
  • 41
2

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.

avance
  • 1,993
  • 2
  • 21
  • 23
1

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

AustinRathe
  • 671
  • 1
  • 10
  • 21
0

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.

Fostah
  • 11,398
  • 10
  • 46
  • 55