Not sure if I hit an error in WebKit or I am doing something terribly wrong, but I cannot figure out how to use WKScriptMessageHandler without causing a leak due to the value contained in WKScriptMessage.body .
I managed to put together a minimal Mac project to isolate the problem, but to no avail.
In the main view controller:
class ViewController: NSViewController { var webView: WKWebView? override func viewDidLoad() { super.viewDidLoad() let userContentController = WKUserContentController() userContentController.addScriptMessageHandler(self, name: "handler") let configuration = WKWebViewConfiguration() configuration.userContentController = userContentController webView = WKWebView(frame: CGRectZero, configuration: configuration) view.addSubview(webView!) let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") let url = NSURL(fileURLWithPath: path!)! webView?.loadRequest(NSURLRequest(URL: url)) } } extension ViewController: WKScriptMessageHandler { func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) { print(message.body) } }
And then in the index.html file:
<html> <head></head> <body> <script type="text/javascript"> webkit.messageHandlers.handler.postMessage("Here a random number for you: " + Math.random() * 10) </script> </body> </html>
When I run the project, open the memory debugger in the Tools application, I see the following leak:

If I add a button that reloads the request, and does it several dozen times, the amount of application memory continues to grow and drops after a certain threshold. This may take some time before crashing in this minimal example, but in my application, where I receive several messages per second, it takes less than 10 seconds to crash.
The whole project can be downloaded here .
Any idea what is going on?
ios memory-leaks webkit macos javascriptcore
Reda lemeden
source share