2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EXC_BAD_ACCESS KERN_INVALID_ADDRESS in UIWebView

Last updated at Posted at 2019-03-26

One of the common crashes has a trace like the following:

0  libobjc.A.dylib                0x1de214d70 objc_msgSend + 16
1  UIKitCore                      0x20c6f0f04 -[UIWebView webView:resource:canAuthenticateAgainstProtectionSpace:forDataSource:] + 92
2  WebKitLegacy                   0x1e92c624c CallResourceLoadDelegateReturningBoolean(bool, void (*)(), WebView*, objc_selector*, objc_object*, objc_object*, objc_object*) + 76
3  WebCore                        0x1e8977604 WebCore::ResourceLoader::canAuthenticateAgainstProtectionSpace(WebCore::ProtectionSpace const&) + 64
4  WebCore                        0x1e8977580 WebCore::ResourceLoader::canAuthenticateAgainstProtectionSpaceAsync(WebCore::ResourceHandle*, WebCore::ProtectionSpace const&, WTF::CompletionHandler<void (bool)>&&) + 32
5  WebCore                        0x1e7ec9160 WTF::Function<void ()>::CallableWrapper<-[WebCoreResourceHandleAsOperationQueueDelegate connection:canAuthenticateAgainstProtectionSpace:]::$_3>::call() + 136
6  JavaScriptCore                 0x1e633cc8c WTF::dispatchFunctionsFromMainThread() + 308
7  Foundation                     0x1dfae142c __NSThreadPerformPerform + 336
8  CoreFoundation                 0x1defbe0e0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
9  CoreFoundation                 0x1defbe060 __CFRunLoopDoSource0 + 88
10 CoreFoundation                 0x1defbd944 __CFRunLoopDoSources0 + 176
11 CoreFoundation                 0x1defb8810 __CFRunLoopRun + 1040
12 CoreFoundation                 0x1defb80e0 CFRunLoopRunSpecific + 436
13 WebCore                        0x1e7e293e8 RunWebThread(void*) + 592
14 libsystem_pthread.dylib        0x1dec4825c _pthread_body + 128
15 libsystem_pthread.dylib        0x1dec481bc _pthread_start + 48
16 libsystem_pthread.dylib        0x1dec4bcf4 thread_start + Th4

The error is Crashed: WebThread EXC_BAD_ACCESS KERN_INVALID_ADDRESS. This usually occurs when you try to access an already released object. The scenario goes something like that:

  1. User enters screen with UIWebView. The UIViewController sets self as the delegate.
  2. Webpage starts downloading
  3. User exits screen and UIViewController gets allocated.
  4. UIWebView finished loading and sends message to delegate method like webViewDidFinishLoad

The delegate of UIWebView is assigned instead of weak. From Apple's official document for UIWebViewDelegate,

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.

Therefore, the solution is we need to stop the UIWebView from loading its page and sets its delegate to nil before we deallocate the delegate.

- (void)dealloc {
    if (self.webview.loading) {
        [self.webview stopLoading];
    }
    self.webview.delegate = nil;
}

reference: https://stackoverflow.com/questions/1520674/exc-bad-access-in-uiwebview

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?