views:

799

answers:

6

I just downloaded the crash reports for one of my iPhone apps from iTunes Connect. The most common crash has a trace like the following:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xa1b1c1db
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                 0x3030e6f4 objc_msgSend + 16
1   UIKit                           0x30ebebee -[UIWebView webView:resource:didFinishLoadingFromDataSource:]
2   UIKit                           0x30ebe5ca -[UIWebViewWebViewDelegate webView:resource:didFinishLoadingFromDataSource:]
3   CoreFoundation                  0x32b73b5c __invoking___ + 60
4   CoreFoundation                  0x32bc67be -[NSInvocation invoke]
5   WebCore                         0x320baa86 HandleDelegateSource
6   CoreFoundation                  0x32bb8a96 CFRunLoopRunSpecific
7   CoreFoundation                  0x32bb8356 CFRunLoopRunInMode
8   GraphicsServices                0x30544cd4 GSEventRunModal
9   GraphicsServices                0x30544d80 GSEventRun
10  UIKit                           0x30d2c768 -[UIApplication _run]
11  UIKit                           0x30d2b46c UIApplicationMain

I'm about 80% sure this is an issue internal to UIWebView and outside the scope of what I can address. Does anyone have any suggestions on how to narrow down this issue to help identify whether it's an issue with the OS and UIWebView, or an issue that I can fix and address in my code? Thanks in advance.

UPDATE: The UIWebView in question is in a view that gets released when the user hits the back button of the corresponding navigation controller. The accepted solution seems to provide a good explanation for why this error is occurring.

Before suggested solution:

- (void)dealloc {
    [webView release];

    [super dealloc];
}

After suggested solution:

- (void)dealloc {
    webView.delegate = nil;
    [webView stopLoading];
    [webView release];

    [super dealloc];
}
+2  A: 

It's almost 100% an error in your code. Errors in the iPhone SDK are quite rare and UIWebView has been tested quite good by many other developers.

EXC_BAD_ACCESS occurs usually when you try to access an already released object. Obviously if Apple's code is trying to do that you are the one who has released the object wrongly. Are you sure you don't have a -autorelease or -release too much?

Georg
reminds me of the Anchorman quote "60% of the time it works every time"
slf
A: 

Take a harder look inside the thing in your code that is implementing the UIWebViewDelegate protocol. In particular you want to look at whatever is handling webViewDidFinishLoad: You are trying to access a variable that's been released. Post the full source if you can, that will help us find it for you.

slf
+9  A: 

The scenario goes something like this:

  1. User enters screen with UIWebView. The UIViewController sets self as the delegate
  2. Web page starts downloading
  3. User exits screen 3a. UIViewController gets deallocated
  4. UIWebView finishes loading and sends "I finished" message to its delegate...

You need to stop the UIWebView from loading its page and sets its delegate to nil before you deallocate the delegate.

Stephen Darlington
I will put money on a released delegate being the cause of this.
Roger Nolan
This explanation fits perfectly - thanks! The dealloc method for the view that contains the UIWebView currently calls (where webView is a UIWebView* field in the class): `[webView release];` I will now have it do this: `webView.delegate = nil;` `[webView stopLoading];` `[webView release];`
Aaron
veyr helpful, thanks.
culov
A: 

Great thread - I had the exact same problem, and the accepted answer almost worked, but I had to change the [webview release] to [webview autorelease] in order to make things work 100%

Todd
A: 

I had a similar problem. I was using: [webView loadHTMLString:str baseURL:tmpUrl];

[str release];

The release of "str" caused the error message "EXC_BAD_ACCESS"

axe
A: 

I had the same problem as well. IMO this should be handled in UIWebView's dealloc().

Robert Hawkey