![](/img/trans.png)
[英]Why did my app crash when assigning self to __weak local under ARC?
[英]app crashing when assigning self to __weak in ARC?
我們在同一個名為“MyViewController”的ViewController中
即使它崩潰也很難再現,但我們從崩潰中得到了這個崩潰......
- (void)dismiss {
MyViewController* __weak weakSelf = self;---->//Crashing here
[UIView animateWithDuration:0.25 animations:^{
MyViewController* __strong strongSelf = weakSelf;
if (strongSelf) {
CGRect backFrame = strongSelf.shroud.frame;
strongSelf.shroud.backgroundImageView.frame = backFrame;
strongSelf.shroud.shadedOverlay.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
CGRect semiViewFrame = CGRectMake(0, strongSelf.view.frame.size.height, strongSelf.shroud.contentView.frame.size.width, strongSelf.shroud.contentView.frame.size.height);
strongSelf.shroud.contentView.frame = semiViewFrame;
}
}
completion:^(BOOL finished){
MyViewController* __strong strongSelf = weakSelf;
if (strongSelf) {
[strongSelf.shroud removeFromSuperview];
strongSelf.shroud = nil;
strongSelf.semiViewController = nil;
}
}];}
我們從crashlytics獲取以下崩潰日志:
Thread : Crashed: com.apple.main-thread
0 libobjc.A.dylib 0x39d148f8 _objc_trap() + 18446744073709552000
1 libobjc.A.dylib 0x39d1495d _objc_inform
2 libobjc.A.dylib 0x39d233cb weak_register_no_lock + 182
3 libobjc.A.dylib 0x39d236ff objc_storeWeak + 110
4 applicationName 0x000e8071 -[MyViewController dismiss] (MyViewController:144)
5 UIKit 0x31f357f9 _UIGestureRecognizerSendActions + 196
6 UIKit 0x31de0a93 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 1138
7 UIKit 0x321880bf ___UIGestureRecognizerUpdate_block_invoke + 46
8 UIKit 0x31da794f _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 218
9 UIKit 0x31da60b3 _UIGestureRecognizerUpdate + 298
10 UIKit 0x31ddf305 -[UIWindow _sendGesturesForEvent:] + 772
11 UIKit 0x31ddec2b -[UIWindow sendEvent:] + 666
12 UIKit 0x31db3e55 -[UIApplication sendEvent:] + 196
13 UIKit 0x31db2521 _UIApplicationHandleEventQueue + 7120
14 CoreFoundation 0x2f548faf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
15 CoreFoundation 0x2f548477 __CFRunLoopDoSources0 + 206
16 CoreFoundation 0x2f546c67 __CFRunLoopRun + 630
17 CoreFoundation 0x2f4b1729 CFRunLoopRunSpecific + 524
18 CoreFoundation 0x2f4b150b CFRunLoopRunInMode + 106
19 GraphicsServices 0x344106d3 GSEventRunModal + 138
20 UIKit 0x31e12871 UIApplicationMain + 1136
請幫我解決這個問題....
提前致謝
你不需要在這里做強/弱舞,因為沒有保留周期。 該塊具有對該視圖的強引用,但該視圖沒有對該塊的強引用。 動畫結束后,塊會釋放視圖。
Nanda請改變你的代碼
MyViewController* __strong weakSelf = self;
通過使這個弱,你試圖訪問weakSelf,而不是內存,你正在崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.