在ARC下将self分配给__weak时,应用程序崩溃?

4
我们在同一个名为“MyViewController”的视图控制器中,尽管它崩溃很难复现,但我们从Crashlytics获得了这个崩溃...
- (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

请帮我解决这个问题...非常感谢!
提前致谢。

使弱点保持 - Arun
@nanda 将名称更改为weakSelf或使用strong。 - Ilesh P
@llesh,您能否稍微描述一下……在哪里更改名称? - Nanda
@Nanda 喜欢 _strong weakSelf - Ilesh P
你能给我们提供Crashlytics的直接分享链接吗? - Tommy
@Nanda 为什么你需要强引用?为什么不能使用__weak引用?还有,为什么你在完成块中检查nil? - Keenle
2个回答

1

你不需要在这里进行强弱引用的操作,因为没有保留周期。块对视图有强引用,但视图没有对块有强引用。当动画结束时,块会释放视图。


-1

Nanda,请将您的代码更改为以下内容

MyViewController* __strong weakSelf = self;

通过将此变量设置为weak,您试图访问weakSelf,但由于其未被加载到内存中,因此导致了崩溃。


1
如果我使用 strong,可能会导致保留循环,因此为了避免这种情况,我使用了 weak 引用。 - Nanda
如果您需要复制 weakSelf,我不确定这只是一个建议。 - Gajendra Rawat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接