EXC_BAD_ACCESS KERN_INVALID_ADDRESS 用于 WKWebView dealloc

EXC_BAD_ACCESS KERN_INVALID_ADDRESS for WKWebView dealloc

为什么下面的堆栈跟踪会因为 WKWebView 而抛出 EXC_BAD_ACCESS KERN_INVALID_ADDRESS?我也不知道这是哪里发生的。

我不知道这是哪里发生的,但这是我在我的一个 nib 中对 WKWebView 的引用。

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

您 运行 是 iOS 11 或更高版本上的应用程序吗? 由于 [WKWebView initWithCoder:] 中存在仅在 iOS 11 中修复的错误,导致崩溃。

在 iOS11 之前,我们应该像下面这样创建 WkWebView 的实例:

    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)

检查此代码:

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
}()

WebKit 仍然尝试调用已释放的对象 0x2aff496d WKWebView。

你的 IBOutlet 有 strong 属性,没必要,因为 nib 在它的子视图上保持强引用。

尽量让属性变弱:

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

您需要将 WebKit 框架添加到您的 Target。

这似乎是 Firebase Admob 的问题。添加以下修复它,此后没有问题。

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

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