Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 12+ Memory Leak - WKWebView and UIWebView

This is not a question, but I'm posting here in the hope that it will save someone else's sanity (I've just spent the best part of a day figuring this one out). I've identified a memory leak that has appeared from iOS 12.0+, which affects WKWebView and UIWebView. The leak appears as soon as you instantiate either of the 2 webviews. Instruments identifies the leak as coming from JavaScriptCore, which I guess is why it affects both webviews equally.

I was trying to figure out what I was doing wrong, when I decided to try an older iOS version (11.4 - the version before 12.0), and I noticed the leak had disappeared. I can reproduce this every time.

To reproduce, all you need to do is allocate an instance of a webview on a class.

let webview = WKWebView()

or

let webview = UIWebView()

Immediately, you'll notice 4 leaks on iOS 12.0/12.1, which are no longer present if you run the code on iOS 11.4. There are 4 separate leaks; 3 x 96 bytes and 1 x 128 bytes.

Leak-WKWebView

I've filed a bug with Apple via the BugReporter, duplicated at Openradar: https://openradar.appspot.com/radar?id=6132657108811776

like image 930
CPR Avatar asked Nov 07 '18 10:11

CPR


People also ask

What is the difference between UIWebview and WKWebView?

Difference Between UIWebview and WKWebViewUIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

Is WKWebView secure?

The WKWebView is a modern API applying all the modern web security mechanisms, it's still maintained by Apple and gets updates. The good thing about WKWebView is that it does out-of-process rendering, so if the attackers find a memory corruption vulnerability in it, your application's process is still isolated.

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

Is WKWebView deprecated?

Since then, we've recommended that you adopt WKWebView instead of UIWebView and WebView — both of which were formally deprecated. New apps containing these frameworks are no longer accepted by the App Store.


1 Answers

Putting it here just for reference (if someone has similar issues).

It seems there is an other, still active memory leak bug in all WKWebview versions with evaluateJavascript https://bugs.webkit.org/show_bug.cgi?id=215729

I have built a workaround for it by using WebSocket communication as an alternative way to send messages to the webview.

If you need it in form of a library (Swift package) https://github.com/Danesz/Sidewalk

(The package is a PoC, but it can help you overcome the issue, and fine-tune it for your own case)

like image 143
danesz Avatar answered Sep 22 '22 23:09

danesz