呈现模态UIViewController时发生奇怪的崩溃
问题描述:
我已经看过这个崩溃报告几次了。这是非常随机和罕见的,我无法理解它。所有我做使用下面的代码呈现模态UIViewController时发生奇怪的崩溃
ComposeController *newcontrol = [[ComposeController alloc]initWithMode:1 withNIB:@"ComposeController"];
newcontrol.delegate = self;
UINavigationController *holder = [[UINavigationController alloc] initWithRootViewController:newcontrol];
[self presentViewController:holder animated:YES completion:NULL];
不知怎的,这完全导致了这种随机的呈现模式视图控制器:
OS Version: iPhone OS 6.1 (10B143)
Report Version: 104
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x9
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x3b25c5d0 objc_msgSend + 16
1 CoreFoundation 0x334ba73f -[__NSPlaceholderArray initWithObjects:count:] + 271
2 CoreFoundation 0x334bae09 +[NSArray arrayWithObject:] + 45
3 UIKit 0x353e80ab -[UIWindow _rotationViewControllers] + 51
4 UIKit 0x353e7fe3 -[UIViewController viewControllerForRotation] + 91
5 UIKit 0x353e7f39 -[UIViewController _visibleView] + 97
6 UIKit 0x3546c05b -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 2483
7 UIKit 0x3546afab -[UIViewController presentViewController:withTransition:completion:] + 3399
8 MyApp 0x00046e97 -[Inbox composeHit] (Inbox.m:207)
答
尝试这样做
[持有人pushViewController:newcontrol动画:YES完成:NULL];
答
我的问题非常一致。这似乎是由于我正在从弹出窗口展示模式视图控制器,而这并未经过充分测试,并触发了Apple代码中的错误。错误在于UIKit保留了一个对我的视图控制器的未被引用的引用,这个引用已经被解散和释放,所以在稍后的时间引用这个引用。我的解决方法是避免从弹出窗口中显示模态VC,或者无限期地保留所有此类VC(在成员变量或某些类型中)。
下面是血淋淋的细节。
这是我用来触发问题的代码。
-(void) handlePinchFromCell:(AMPSupportingPhotoCell*) cell
{
UIViewController * vc = [[UIViewController alloc] init]; // UIViewController #0
[self presentViewController:vc animated:YES completion:nil];
[self performSelector:@selector(dismiss:) withObject:vc afterDelay:2];
}
-(void) dismiss:(UIViewController*)vc
{
[self dismissViewControllerAnimated:YES completion:^(){NSLog(@"-------");}];
}
此代码是一个UIViewController#1的一部分,这是一个UIPopoverController内部,从另一个的UIViewController#2,其是本身从又另一个3视图的UIViewController#所呈现弹出过来。崩溃发生后关闭,控制器#2 id被解散。
如果我让僵尸,我会得到相同的堆栈跟踪,但有一个消息:
2013-03-13 20:04:24.681 Mercury[16698:19d03] handlePinchFromCell: a225710
2013-03-13 20:04:27.083 Mercury[16698:19d03] -------
2013-03-13 20:04:31.606 Mercury[16698:19d03] *** -[UIViewController retain]: message sent to deallocated instance 0xa225710
所以,注意到VC#0得到分配,提出了释放,辞退2秒后,竟然有仍然是苹果代码中的一个悬而未决的参考。当popover关闭并且VC#2被解雇时,整个事情就会崩溃,试图访问释放的VC。我相当肯定这是苹果的错误。我也猜想这与从一个popover展示一个VC有关,所以如果你远离那个,或者自己保留VC,你应该没问题。
同一个问题的另一个堆栈跟踪是这样的。它发生如果上面的代码运行两次:
2013-03-13 20:12:53.883 Mercury[16735:19d03] handlePinchFromCell: 16d54da0
2013-03-13 20:12:56.285 Mercury[16735:19d03] -------
2013-03-13 20:13:03.481 Mercury[16735:19d03] handlePinchFromCell: a2565f0
2013-03-13 20:13:03.481 Mercury[16735:19d03] *** -[UIViewController isKindOfClass:]: message sent to deallocated instance 0x16d54da0
(lldb) bt
* thread #1: tid = 0x1f03, 0x017f8a97 CoreFoundation`___forwarding___ + 295, stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
frame #0: 0x017f8a97 CoreFoundation`___forwarding___ + 295
frame #1: 0x017f894e CoreFoundation`_CF_forwarding_prep_0 + 14
frame #2: 0x00c42f90 UIKit`-[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 907
frame #3: 0x00a40ee3 UIKit`-[UIViewController presentViewController:withTransition:completion:] + 4521
frame #4: 0x00a41167 UIKit`-[UIViewController presentViewController:animated:completion:] + 112
frame #5: 0x0006c32d Mercury`-[AMPSupportingPhotosViewController handlePinchFromCell:](self=0x16d55360, _cmd=0x0007a64a, cell=0x0a22a2a0) + 205 at AMPSupportingPhotosViewController.m:184
frame #6: 0x015336b0 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #7: 0x0006f317 Mercury`-[AMPSupportingPhotoCell handlePinch:](self=0x0a22a2a0, _cmd=0x00efb0db, sender=0x0a2504a0) + 215 at AMPSupportingPhotoCell.m:53
frame #8: 0x00c2185a UIKit`_UIGestureRecognizerSendActions + 139
确实有趣。在我的情况下,我从另一个模态视图控制器呈现模态视图控制器。你认为同样的问题?他们总是按照正确的顺序释放,但是根据你所说的话,似乎如果有人说在第一个模态控制器上点击取消,然后在原始模态控制器上迅速取消可能触发崩溃的取消? – 2013-03-15 05:00:51
我认为这是一回事 - 一个VC陷入苹果系统的某个地方。 – DenNukem 2013-03-18 20:58:18