WKWebView: userContentController leaks script message handlers if you do not explicitly remove them before the web view is deallocated

I have an object which I add to WKWebview's userContentController as a script message handler:


  [userContentController addScriptMessageHandler:myScriptMessageHandler name:@"handlerName"];


Now, I can see that the web view deallocates when the view controller it is in gets dismissed. But myScriptMessageHandler does not deallocate. Instruments doesn't pick up a leak either. If I call removeScriptMessageHandlerForName: inside my WKWebview subclasses dealloc method, the script message handler deallocates.


Is there any particular reason why script message handlers wouldn't get destroyed when the WKWebview is deallocated?

Replies

Damned if you do and damned if you don't. It turns out that WKWebView doesn't fully deallocate itself. About 30 seconds after you try to deallocate it it generates a call to it's cookie something-or-other and generates a strange error. Search for the error "Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service" or go directly to stackoverflow 44585980