iOS 8中使用UIAlertController或UIActionSheet时出现内存泄漏问题

9
我在模拟器中使用UIActionSheet或UIAlertController进行以下操作时,发现iOS 8存在内存泄漏问题。由于iOS 8中UIActionSheet使用了UIAlertController,因此这些问题是相关的。
当按钮被按下时,将调用showCameraAction方法。即使我从委托方法中删除了所有内容,仍然会在下面显示的情况下出现泄漏。我是否以某种不应该使用UIActionSheet的方式使用了它?希望能获得任何有助于解决此问题的帮助。相同的代码在iOS 7(模拟器中)中没有泄漏。
-(IBAction)showCameraAction:(id)sender
{

UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Photo From:"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"Phone", @"Flickr", nil];

[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
//also tried  just showInView: self.view
}

抱歉,您需要提供需要翻译的内容。
 - (void)actionSheet:(UIActionSheet *)actionSheet
 clickedButtonAtIndex:(NSInteger)buttonIndex {
 }

我也尝试了使用UIAlertController,结果相同:

UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:@"Photo From:"
                                      message:@""
                                      preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *phoneAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"Phone", @"Phone action")
                               style:UIAlertActionStyleCancel
                               handler:^(UIAlertAction *action)
                               {
                                   NSLog(@"Phone action");
                               }];

UIAlertAction *flickrAction = [UIAlertAction
                           actionWithTitle:NSLocalizedString(@"Flickr", @"Flickr action")
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction *action)
                           {
                               NSLog(@"Flickr action");
                           }];

[alertController addAction:phoneAction];
[alertController addAction:flickrAction];


[self presentViewController:alertController animated:YES completion:nil];

泄漏工具截图

带有跟踪信息的截图: https://www.dropbox.com/l/FmnTCd0PvVhuu16BVHZo7p


我在使用UIAlertController和UIAlertView时遇到了类似的泄漏问题:https://dev59.com/4F8d5IYBdhLWcg3w8F6- - MartinMoizard
http://stackoverflow.com/questions/26277614/uiactionsheet-button-text-not-displaying-properly-in-ios-8 - Waseem Shah
@Jageen 是的。我正在使用ARC。 - Praneeth Wanigasekera
@MartinMoizard 谢谢。这很有趣。看起来我的UIActionSheet在iOS 8中也使用了UIAlertController,所以它们似乎是相关的。 - Praneeth Wanigasekera
我也遇到了这个问题!它在真正的iPad上泄漏,而模拟器只在iOS8.x上。请查看我在苹果论坛中的留言https://devforums.apple.com/message/1099436#1099436 - cat
如果您在iOS上遇到了可重现的错误,请向苹果报告:https://bugreport.apple.com - Robotic Cat
4个回答

5

我建议在iOS8中使用“UIAlertController”,并通过“UIAlertAction”块触发任何事件时,从呈现的控制器中解散alertController对象。

UIAlertController  *alertController = [UIAlertController alertControllerWithTitle:@"title"
message:@"message"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction  *alertAction = [UIAlertAction actionWithTitle:@"actionTitle"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//Do ur stuff
[alertController dismissViewControllerAnimated:YES
                                    completion:NULL];
}];
[alertController addAction:alertAction];
[self presentViewController:alertController
                       animated:YES
                     completion:NULL];

4

这是一个iOS的bug。

请参考苹果Bug报告问题21005708,在ARC下UIAlertController存在内存泄漏问题。


1
这不是答案,而是超过评论的泄漏证据。希望它能帮助找到解决方案或解决方法。泄漏似乎是iPad 3 / Retina上特定设备的问题!我通过覆盖视图控制器的保留和释放来进行了一些测试,以显示iOS 8.x中的泄漏。另请参阅:https://devforums.apple.com/message/1099577#1099577
  • 泄漏设备:iPad 3(A1416),iPad Air模拟器
  • 良好设备:iPhone 6 iOS 8.1.3,带有iOS 8.1.2的iPhone 4s
AGC是视图控制器。正确的保留计数应为2。

iPad Retina模拟器iOS 8.1和真实的iPad泄漏

// 第二次运行...这次选择一个选项并发生了泄漏 12:56:50.929 SimplySolitaire[27643:473670] >>> 将actionSheet showInView:retain = 2 12:56:50.930 SimplySolitaire[27643:473670] AGC retain == 3 12:56:50.950 SimplySolitaire[27643:473670] AGC retain == 4 12:56:50.951 SimplySolitaire[27643:473670] AGC retain == 5 12:56:50.951 SimplySolitaire[27643:473670] AGC retain == 6 12:56:50.951 SimplySolitaire[27643:473670] <<< DID actionSheet showInView:retain = 6 12:56:50.998 SimplySolitaire[27643:473670] AGC release = 5 12:56:51.042 SimplySolitaire[27643:473670] AGC release = 4 12:56:51.042 SimplySolitaire[27643:473670] AGC release = 3 // 用户通过点击按钮关闭操作表(委托为nil) 12:56:53.257 SimplySolitaire[27643:473670] AGC retain == 4 12:56:53.257 SimplySolitaire[27643:473670] AGC retain == 5 12:56:53.258 SimplySolitaire[27643:473670] AGC retain == 6 12:56:53.258 SimplySolitaire[27643:473670] AGC retain == 7 12:56:53.258 SimplySolitaire[27643:473670] AGC release = 6 12:56:53.259 SimplySolitaire[27643:473670] AGC release = 5 12:56:53.612 SimplySolitaire[27643:473670] AGC release = 4 12:56:53.612 SimplySolitaire[27643:473670] AGC release = 3 // <<<<<<<<<< 泄漏应该是2 // 最后一个释放丢失了,但只有iOS系统代码被执行。 12:54:54.757 SimplySolitaire[27643:473670] >>>将actionSheet showInView:retain = 2 12:54:54.758 SimplySolitaire[27643:473670] AGC保留== 3 12:54:54.798 SimplySolitaire[27643:473670] AGC保留== 4 12:54:54.798 SimplySolitaire[27643:473670] AGC保留== 5 12:54:54.798 SimplySolitaire[27643:473670] AGC保留== 6 12:54:54.798 SimplySolitaire[27643:473670] <<< DID actionSheet showInView:retain = 6 12:54:54.845 SimplySolitaire[27643:473670] AGC释放= 5 12:54:54.891 SimplySolitaire[27643:473670] AGC释放= 4 12:54:54.891 SimplySolitaire[27643:473670] AGC释放= 3 // 现在...没有选择的情况下解散操作表(委托为空) 12:55:05.643 SimplySolitaire[27643:473670] AGC保留== 4 12:55:05.644 SimplySolitaire[27643:473670] AGC保留== 5 12:55:05.644 SimplySolitaire[27643:473670] AGC保留== 6 12:55:05.644 SimplySolitaire[27643:473670] AGC保留== 7 12:55:05.645 SimplySolitaire[27643:473670] AGC释放= 6 12:55:05.645 SimplySolitaire[27643:473670] AGC释放= 5 12:55:05.996 SimplySolitaire[27643:473670] AGC释放= 4 12:55:05.997 SimplySolitaire[27643:473670] AGC释放= 3 12:55:05.997 SimplySolitaire[27643:473670] AGC释放= 2 //这是正确的保留值为2

-1

我建议您切换到UIAlertController。UIActionSheet在iOS 8中已被弃用,因此您可以尝试一下并查看是否仍然存在泄漏问题。


谢谢!我之前不知道这个问题,而且xCode也没有警告我。然而,根据这个链接,UIAlertController似乎也有同样的问题:https://dev59.com/4F8d5IYBdhLWcg3w8F6- - Praneeth Wanigasekera
2
确认使用UIAlertController会出现相同的内存泄漏问题。 - Praneeth Wanigasekera
你解决了这个问题吗,Praneeth?只是想知道是否还有其他因素(这就是为什么在你切换到UIAlertController之后它仍然发生的原因)。 - Peter Johnson

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