在Object C循环引用是大问题,稍微不注意就会出现循环引用导致泄露,副作用是导致崩溃,尤其是使用Block的时候
__weakObj(commonH5PagePanel)
[commonH5PagePanel.jsBridge registerMethod:@"common_HN_changeFrmSize" handler:^(NSDictionary *ps, SHJSBridgeSendResponse callback) {
int width = [(NSNumber *)ps[@"width"] intValue];
int height = [(NSNumber *)ps[@"height"] intValue];
__strongObj(commonH5PagePanel)
/**
这块循环引用要注意,如果不进行弱引用处理,这个block就强引用了commonH5PagePanel,
而,commonH5PagePanel本身就强引用了block,这样就形成了循环引用,会导致两者都不能释放。发现了内存泄露
进一步导致的问题是,ViewController dealloc的时候,commonH5PagePanel不能被销毁,而且状态已经不对,
但是block再次回调执行mas_updateConstraints的时候,就崩溃了。
解决办法就是,采用弱引用,ViewController dealloc的时候,commonH5PagePanel被销毁,block也被销毁,
就不存在,在错误状态的时候block回调导致的崩溃了。
*/
[commonH5PagePanel mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@(width));
make.height.equalTo(@(height));
make.right.equalTo(@(-10));
}];
}];
#define __weakObj(obj) __weak typeof(obj)weak##obj = obj;
#define __strongObj(obj) __strong typeof(weak##obj)obj = weak##obj;
崩溃信息如下:
2020-05-20 10:19:34.245608+0800 SHPlayer[50927:5212491] [General] (
0 CoreFoundation 0x00007fff2d826d07 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff6654a5bf objc_exception_throw + 48
2 Foundation 0x00007fff2fe50687 ResolveConstraintArguments + 766
3 Foundation 0x00007fff2fe502c1 +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] + 80
4 Masonry 0x00000001007647a1 -[MASViewConstraint install] + 847
5 Masonry 0x0000000100762412 -[MASConstraintMaker install] + 678
6 Masonry 0x0000000100767de0 -[NSView(MASAdditions) mas_updateConstraints:] + 142
7 SHPlayer 0x00000001000df1c5 __57-[SHMovieViewController(H5) showVideoHistoryPageWithTop:]_block_invoke_3 + 341
8 MRFoundation 0x00000001006c2cf8 -[MRWebViewJSBridge invokeNativeMethod:parameter:completion:] + 328
9 MRFoundation 0x00000001006c2f9a -[MRWebViewJSBridge invokeNativeMethod:parameter:callBack:mid:] + 410
10 MRFoundation 0x00000001006c3788 -[MRWebViewJSBridge handleH5Message:callBack:] + 1288
11 MRFoundation 0x00000001006c79fb -[MRWKWebView userContentController:didReceiveScriptMessage:] + 299
12 WebKit 0x00007fff3e886cbf _ZN28ScriptMessageHandlerDelegate14didPostMessageERN6WebKit12WebPageProxyEONS0_13FrameInfoDataERN7WebCore21SerializedScriptValueE + 207
13 WebKit 0x00007fff3ea3d3a6 _ZN6WebKit29WebUserContentControllerProxy14didPostMessageERN3IPC10ConnectionEN3WTF16ObjectIdentifierINS_26WebPageProxyIdentifierTypeEEEONS_13FrameInfoDataEyRKNS1_13DataReferenceE + 200
14 WebKit 0x00007fff3ec08c34 _ZN6WebKit29WebUserContentControllerProxy17didReceiveMessageERN3IPC10ConnectionERNS1_7DecoderE + 150
15 WebKit 0x00007fff3e6ceff2 _ZN3IPC18MessageReceiverMap15dispatchMessageERNS_10ConnectionERNS_7DecoderE + 114
16 WebKit 0x00007fff3e990500 _ZN6WebKit15WebProcessProxy17didReceiveMessageERN3IPC10ConnectionERNS1_7DecoderE + 28
17 WebKit 0x00007fff3e6bbd9d _ZN3IPC10Connection15dispatchMessageENSt3__110unique_ptrINS_7DecoderENS1_14default_deleteIS3_EEEE + 209
18 WebKit 0x00007fff3e6bb701 _ZN3IPC10Connection24dispatchIncomingMessagesEv + 475
19 JavaScriptCore 0x00007fff31f38ef7 _ZN3WTF7RunLoop11performWorkEv + 551
20 JavaScriptCore 0x00007fff31f390aa _ZN3WTF7RunLoop11performWorkEPv + 26
21 CoreFoundation 0x00007fff2d7aaf12 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
22 CoreFoundation 0x00007fff2d7aaeb1 __CFRunLoopDoSource0 + 103
23 CoreFoundation 0x00007fff2d7aaccb __CFRunLoopDoSources0 + 209
24 CoreFoundation 0x00007fff2d7a99fa __CFRunLoopRun + 927
25 CoreFoundation 0x00007fff2d7a8ffe CFRunLoopRunSpecific + 462
26 HIToolbox 0x00007fff2c3dcabd RunCurrentEventLoopInMode + 292
27 HIToolbox 0x00007fff2c3dc7d5 ReceiveNextEventCommon + 584
28 HIToolbox 0x00007fff2c3dc579 _BlockUntilNextEventMatchingListInModeWithFilter + 64
29 AppKit 0x00007fff2aa27c99 _DPSNextEvent + 883
30 AppKit 0x00007fff2aa264e0 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
31 AppKit 0x00007fff2aa181ee -[NSApplication run] + 658
32 AppKit 0x00007fff2a9e9ff6 NSApplicationMain + 777
33 SHPlayer 0x000000010009e482 main + 34
34 libdyld.dylib 0x00007fff676f1cc9 start + 1
35 ??? 0x0000000000000003 0x0 + 3
)