title: Memory leak when using WKScriptMessageHandler
body: <p>Not sure if I hit a bug in <code>WebKit</code> or I am doing something horribly wrong, but I can't figure out how to use <code>WKScriptMessageHandler</code> without causing whatever value contained in <code>WKScriptMessage.body</code> to leak.</p>
  
  <p>I was able to put together a minimum Mac project to isolate the issue, but to no avail.</p>
  
  <p>In the main view controller:</p>
  
  <pre><code>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)
     }
  }
  </code></pre>
  
  <p>And then in the <code>index.html</code> file: </p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;&lt;/head&gt;
    &lt;body&gt;
      &lt;script type="text/javascript"&gt;
        webkit.messageHandlers.handler.postMessage("Here's a random number for you: " + Math.random() * 10)
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I run the project then open the memory debugger in Instruments, I see the following leak:</p>
  
  <p><img src="https://i.stack.imgur.com/xnoQf.png" alt="leak"></p>
  
  <p>If I add a button that reloads the request, and do so few dozen times, the memory footprint of the app keeps growing, and crashes after a certain threshold. It might take a while before crashing in this minimal example, but in my app where I receive several messages per second, it takes less than 10s to crash.</p>
  
  <p>The whole project can be <a href="https://github.com/kaishin/TestWebkitMessages/archive/master.zip" rel="noreferrer">downloaded here</a>.</p>
  
  <p>Any idea of what's going on?</p>