Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UIWebView leaked

class MyViewController: UIViewController {
     @IBOutlet weak var webView: UIWebView?
     override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: urlString)
    let request = NSURLRequest(URL: url!)
    SVProgressHUD.show()

    webView?.loadRequest(request)
    webView?.scrollView.header = MJRefreshNormalHeader(refreshingBlock: {

      [weak self] in
      if let strongSelf = self {
        strongSelf.webView?.reload()
      }})   }
     override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated) 

    SVProgressHUD.dismiss()   }    }

extension MyViewController: UIWebViewDelegate {
     func webViewDidFinishLoad(webView: UIWebView) {
    webView.scrollView.header.endRefreshing()
    SVProgressHUD.dismiss()   }       func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
    webView.scrollView.header.endRefreshing()
    SVProgressHUD.dismiss()   }    }

The view controller was pushed by a navigation controller, when I pop it, I got leaks. In instrument.Leak I saw these.

#

Leaked Object   #   Address Size    Responsible Library Responsible Frame NSMutableArray    1   0x137a6ddb0 48 Bytes    UIKit   -[_UIKeyboardTextSelectionGestureController init]
_UIKeyboardTextSelectionController  1   0x137a6e800 96 Bytes    UIKit   -[UIWebSelectionAssistant addNonEditableForceTextSelectionGestureRecognizersToView:]
_UIKeyboardBasedNonEditableTextSelectionGestureController   1   0x137a6dcd0 160 Bytes   UIKit   -[UIWebSelectionAssistant addNonEditableForceTextSelectionGestureRecognizersToView:]

#

I'm sure that the webView, myViewController were delayed, but when pop the myViewController, 4M increased and not release. Please help and thanks.

List item

like image 469
Z.Chris Avatar asked Jul 27 '26 13:07

Z.Chris


1 Answers

According to an answer posted here, there is a workaround that if you set configuration.selectionGranularity to WKSelectionGranularityCharacter, the leaks stop:

let config = WKWebViewConfiguration()
config.selectionGranularity = .character //WKSelectionGranularityCharacter
let myWebview = WKWebview(frame: frame, configuration: config)

This worked for me, but then, when selecting text, there was no selection rectangle in the webview. This may or may not be a viable workaround in your case.

Edit I just noticed your question is for UIWebView, not WKWebView. It doesn't look like you can set this on UIWebView. I'll leave this answer for now since WKWebView folks googling this memory leak will probably find this thread...

like image 191
Jon Brooks Avatar answered Jul 30 '26 03:07

Jon Brooks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!