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