Memory leaks from Wkwebview and JS interactions

Source: Internet
Author: User

Recent development suddenly found Rich text post details memory is not released, looking for a long time the problem has not been found, finally found the problem today, first a bit of code snippet

Wkwebviewconfiguration *configuration =[[Wkwebviewconfiguration alloc] Init];configuration.usercontentcontroller= [WkusercontentcontrollerNew]; [Configuration.usercontentcontroller addscriptmessagehandler:self Name:@"Jumpweibopostimage"]; Wkpreferences*preferences = [wkpreferencesNew]; Preferences.javascriptcanopenwindowsautomatically=NO; Preferences.javascriptenabled=YES; Configuration.preferences=preferences; Wkwebview*webview = [[Wkwebview alloc] Initwithframe:cgrectmake (WEBVIEWX,0, Self.view.width-webviewx *2,1) configuration:configuration]; WebView.scrollView.bounces=NO; WebView.scrollView.scrollEnabled=NO; Webview.uidelegate=Self ; Webview.navigationdelegate=Self ; [Self.headerview Addsubview:webview];

The above fragment all calls the system method, looks like the life to be harmless actually hidden the murder, who also unexpectedly is here to me to engage for many days

The problem comes from Addscriptmessagehandler:name: This method, one of which is passed to self, is an instance of this class, and the following is a guess: the configuration has this class instance and this class has WebView, WebView has a configuration that causes circular references, of course, the premise is that the configuration is a strong reference to this class, and if it is a weak reference, there should be no circular references.

So the first method can be re-launched this page when the call Removescriptmessagehandlerforname: Manually logoff the added method to achieve the effect of releasing the VC.

Now that an instance of the class is passed in when calling Addscriptmessagehandler:name: it will not be released, then you can define a class individually to pass in the class.

@interfaceWeakscriptmessagedelegate:nsobject<wkscriptmessagehandler>@property (nonatomic, weak)ID<WKScriptMessageHandler>scriptdelegate;-(Instancetype) Initwithdelegate: (ID<WKScriptMessageHandler>) scriptdelegate;@end@implementationweakscriptmessagedelegate-(Instancetype) Initwithdelegate: (ID<WKScriptMessageHandler>) scriptdelegate { self=[Super Init]; if(self) {_scriptdelegate=scriptdelegate; }    returnSelf ;}- (void) Usercontentcontroller: (Wkusercontentcontroller *) Usercontentcontroller didreceivescriptmessage: (WKScriptMessage *) Message {[Self.scriptdelegate usercontentcontroller:usercontentcontroller didreceivescriptmessage:message];}@end

At this point, when initializing the WebView registration configuration, it will be modified to

[Configuration.usercontentcontroller addscriptmessagehandler:[[weakscriptmessagedelegate alloc] InitWithDelegate: Self] Name:@ "jumpweibopostimage"];

This is you will find the VC Dealloc method will be called, but there is a problem is that the transition class was not released, at this time in the Dealloc method will webview register the monitoring method removed can

[Self.configuration.userContentController removescriptmessagehandlerforname:@ "jumpweibopostimage "];

By: First Mitsuo

Memory leaks from Wkwebview and JS interactions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.