1

Why would the below stack trace throw a EXC_BAD_ACCESS KERN_INVALID_ADDRESS because of the WKWebView? I can't tell where this is happening either.

I dont know where this is happening but here is my reference to the WKWebView in one of my nibs.

@property (strong, nonatomic) IBOutlet WKWebView *bracketWebView;

StackTrace

Crashed: com.apple.main-thread
0  WebKit                         0x2af40854 WebKit::WebPageProxy::close() + 11
1  WebKit                         0x2aff496d -[WKWebView dealloc] + 120
2  WebKit                         0x2aff496d -[WKWebView dealloc] + 120
3  WebKit                         0x2aff48f1 -[WKWebView initWithCoder:] + 20
4  UIKit                          0x28bc310f UINibDecoderDecodeObjectForValue + 782
5  UIKit                          0x28bc2df5 -[UINibDecoder decodeObjectForKey:] + 296
6  UIKit                          0x28aab4a5 -[UIRuntimeConnection initWithCoder:] + 160
7  UIKit                          0x28bc310f UINibDecoderDecodeObjectForValue + 782
8  UIKit                          0x28bc30a1 UINibDecoderDecodeObjectForValue + 672
9  UIKit                          0x28bc2df5 -[UINibDecoder decodeObjectForKey:] + 296
10 UIKit                          0x28aaaa0b -[UINib instantiateWithOwner:options:] + 1110
11 UIKit                          0x2895daa3 -[UIViewController _loadViewFromNibNamed:bundle:] + 322
12 UIKit                          0x28730ffb -[UIViewController loadView] + 142
13 UIKit                          0x285f6a1f -[UIViewController loadViewIfRequired] + 150
14 UIKit                          0x2860ee71 -[UIViewController __viewWillAppear:] + 124
15 UIKit                          0x287a51e5 -[UINavigationController _startCustomTransition:] + 1040
16 UIKit                          0x286b34a7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 650
17 UIKit                          0x286b31b5 -[UINavigationController __viewWillLayoutSubviews] + 52
18 UIKit                          0x286b312b -[UILayoutContainerView layoutSubviews] + 214
19 UIKit                          0x285f2a73 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 714
20 QuartzCore                     0x2668abcd -[CALayer layoutSublayers] + 128
21 QuartzCore                     0x26686375 CA::Layer::layout_if_needed(CA::Transaction*) + 348
22 QuartzCore                     0x26686209 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
23 QuartzCore                     0x266856d1 CA::Context::commit_transaction(CA::Transaction*) + 368
24 QuartzCore                     0x266853a5 CA::Transaction::commit() + 520
25 QuartzCore                     0x2667eb2b CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 138
26 CoreFoundation                 0x2403d6c9 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
27 CoreFoundation                 0x2403b9cd __CFRunLoopDoObservers + 280
28 CoreFoundation                 0x2403bdff __CFRunLoopRun + 958
29 CoreFoundation                 0x23f8b229 CFRunLoopRunSpecific + 520
30 CoreFoundation                 0x23f8b015 CFRunLoopRunInMode + 108
31 GraphicsServices               0x2557bac9 GSEventRunModal + 160
32 UIKit                          0x2865f189 UIApplicationMain + 144
33 com.exposure.0                 0xbad0d main + 15 (main.m:15)
34 ???                            0x23c33873 (Missing)
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • Are you adding a `WKWebView` in code? Does it have a `strong` reference? – Darren Dec 24 '19 at 23:09
  • I am adding it through a nib and I added the code for the reference in the question. However I dont know if this is where the error is happening but its one of the places where the WKWebView is at, but its the same as the others. I do have firebase that might do things behind the scenes. – Mike Flynn Dec 25 '19 at 16:12
  • Some debugging thoughts: Do you use delegates? If so, are they valid? Can you override `[UIViewController loadView]` in your custom controller and see how things look before calling `[super loadView]`? – Phillip Mills Dec 29 '19 at 21:15
  • That is the thing. I dont know where exactly this is happening since you can see by that stack trace no custom code. – Mike Flynn Dec 29 '19 at 21:48
  • True, but the stack trace still describes an activity that you should be able to intercept. A navigation controller is loading some view controller which, in turn, is loading a WKWebView from a nib. Presumably, you would know which view controllers are capable of doing that and could therefore insert a `loadView` method. – Phillip Mills Dec 29 '19 at 22:37
  • I did super loadView and the thing doesnt even work anymore with loading the website. – Mike Flynn Dec 29 '19 at 23:30
  • Never never never call `super loadView` – matt Dec 30 '19 at 02:14
  • I figured that after this guy told me – Mike Flynn Dec 30 '19 at 02:15
  • FWIW, the point was just to put a `loadView` there long enough to insert a breakpoint and see what was going on. – Phillip Mills Jan 03 '20 at 19:50
  • The crash looks like it is happening on launch – matt Jan 03 '20 at 19:57

5 Answers5

1

Are you running the app on the iOS 11 or above? Since there was a bug in [WKWebView initWithCoder:] that was only fixed in iOS 11, which causes the crash.

Before iOS11, we should create the instance of WkWebView like below:

    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.userContentController.add(self, name: "callbackHandler")
    wkWebView = WKWebView(frame: contentView.bounds, configuration: webConfiguration)
    wkWebView.allowsLinkPreview = false
    wkWebView.translatesAutoresizingMaskIntoConstraints = false
    wkWebView.navigationDelegate = self
    contentView.addSubview(wkWebView)
Lei Zhang
  • 634
  • 4
  • 6
0

Check this code:

lazy var contentWebView: WKWebView = {
    let webView = WKWebView(frame: .zero)
    webView.navigationDelegate = self
    webView.allowsLinkPreview = true
    webView.uiDelegate = self
    webView.backgroundColor = .clear
    webView.scrollView.isScrollEnabled = true
    webView.scrollView.backgroundColor = .clear
    webView.sizeToFit()
    return webView
}()
nitin.agam
  • 1,949
  • 1
  • 17
  • 24
0

WebKit still attempt to call the deallocated object 0x2aff496d WKWebView.

Your IBOutlet has strong attribute, it's not necessary, because nib keep strong reference on it's subviews.

Try to make the attribute weak:

@property (weak, nonatomic) IBOutlet WKWebView *bracketWebView;
George
  • 202
  • 2
  • 8
  • @MikeFlynn, I read the post, but you talking about UINib, so they're still use weak reference. Your object 0x2aff496d WKWebView deallocated and WebKit reference to them. Try handle this by making IBOutlet weak. – George Jan 03 '20 at 20:38
  • Ok but what is the difference between me using a nib/outlet and them using a storyboard/outlet? Isnt that the same thing? They are saying to use a strong property and if you use weak you can get crashes? – Mike Flynn Jan 03 '20 at 23:58
  • @MikeFlynn I'm using Xcode 11. I have dragged UILabel from storyboard to the viewcontroller and UINib to the view and I had have weak reference though in the post used Xcode 9 and reference was strong, may be it's a bug in Xcode 9. The UIViewController has to keep strong reference to the view, because UIViewController doesn't inherited from view and doesn't has UIView hierarchy. And vice versa, UIView has strong reference on it’s subviews addSubview. So, when you assign view with strong attribute it’s redundant. https://developer.apple.com/documentation/uikit/uiview/1622616-addsubview – George Jan 04 '20 at 11:12
  • @MikeFlynn. Back to the question. The crash with log EXC_BAD_ACCESS KERN_INVALID_ADDRESS says literally that INVALID ADDRESS! That means, some object try send message to the unexisten object. – George Jan 04 '20 at 11:13
  • Sorry I am keeping it strong, https://stackoverflow.com/questions/7678469/should-iboutlets-be-strong-or-weak-under-arc/31395938#31395938 – Mike Flynn Jan 05 '20 at 14:49
  • @MikeFlynn it's always depends at the situation(By default I use weak and don't have any problem with that). From yours last link: "The current recommended best practice from Apple is for IBOutlets to be strong unless weak is specifically needed to avoid a retain cycle..." But here you have the crash, perhaps, cause you keep strong reference on dead object (memory location that already contains trash). Have you tried a weak reference to avoid crash? – George Jan 06 '20 at 13:01
0

You need to add the WebKit framework to your Target.

enter image description here

El Tomato
  • 6,479
  • 6
  • 46
  • 75
0

This seemed to be a Firebase Admob issue. Adding the below fixed it, no issues since.

<key>gad_preferred_webview</key>
<string>wkwebview</string>

https://groups.google.com/forum/#!category-topic/google-admob-ads-sdk/ios/I4EEWrPPbSc

Mike Flynn
  • 22,342
  • 54
  • 182
  • 341