apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.16k stars 987 forks source link

CDVViewController leaks CDVWebViewWeakScriptMessageHandler when destroyed #920

Closed j-crosson closed 2 months ago

j-crosson commented 4 years ago

Bug Report

Problem

What is expected to happen?

No leak

What does actually happen?

leak

Information

I’m working on a plugin that uses a subclassed CDVViewController in a modal view. Works fine except for leaking CDVWebViewWeakScriptMessageHandler every time the view goes away. I was able to stop the leak with the following change to CDVViewController.m.

Not that this is the final fix, but it does point out the issue.

In my plugin subclass I use this:

deinit {
    let wkWebView = webViewEngine.engineWebView as! WKWebView
    wkWebView.configuration.userContentController.removeScriptMessageHandler(forName: "cordova")
  }

The ordering is a bit different, but it seems to work.

This would also be an issue when embedding Cordova WebViews in a native app.

A related issue stems from the integration of the SplashScreen plugin into CDVViewController. I’m showing a non-startup view and don’t want to see a startup image (a Webview embedded in a native app would have the same issue) In my subclassed ViewController “viewDidLoad”, I replace “launchView” with a UIVIew with a selectable background color. No flashes or startup images show. Works fine. My issue is that I have to use techniques I’d rather not use to expose “private” class members

Command or Code

Environment, Platform, Device

iOS 13.5.1

Version information

Cordova v9 Cordova-ios 6.1.0 Xcode 11.5 macOS 10.15.5

Checklist

Durzan666 commented 4 years ago

I use a subclass of CDVViewController with a NavigationController and observed that removing CDVViewController from NavigationController results in not calling dealloc because of strong references. I had to remove the strong references such as the MessageHandler in viewWillDisappear so dealloc got called. In short: I copied the whole code of dealloc to viewWillDisappear including the above fix. That fixed the leak of CDVViewController in my case.