iOS 8仅使用UIAlertController或UIActionSheet进行内存泄漏

时间:2014-10-08 00:04:57

标签: ios memory-leaks ios8 uiactionsheet uialertcontroller

当我使用UIActionSheet或UIAlertController执行以下操作时,我在模拟器中看到iOS 8中的内存泄漏。 UIActionSheet在IOS 8中使用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];

Leaks tool screenshot

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

4 个答案:

答案 0 :(得分:5)

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

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];

答案 1 :(得分:4)

这是一个iOS错误。

请参阅suggested问题 21005708 ,ARC下的UIAlertController中的内存泄漏。

答案 2 :(得分:1)

这不是答案,但更多的泄漏证据超出了评论。可能有助于找到解决方案或解决方法。泄漏似乎是iPad 3 / Retina上特定于设备的!

我自己做了一些测试,重写了视图控制器的保留和释放,以显示iOS 8.x中的泄漏

另请参阅:https://devforums.apple.com/message/1099577#1099577

  • LEAKY设备:iPad 3(A1416),iPad Air Simulator
  • GOOD设备:iPhone 6 iOS 8.1.3,iPhone 4s iOS 8.1.2

AGC是视图控制器。正确的保留计数应为2。

iPad Retina模拟器iOS 8.1和真正的iPad LEAK // second run ... this time with LEAK by selecting an option 12:56:50.929 SimplySolitaire[27643:473670] >>> WILL 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 // USER dismisses the action sheet with tapping a button (delegate is 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 // <<<<<<<<<< LEAK should be 2 // the last release is missing, but only iOS system code has executed.

iPad Retina模拟器iOS 8.1和真正的iPad,没有泄漏的解雇 12:54:54.757 SimplySolitaire[27643:473670] >>> WILL actionSheet showInView: retain = 2 12:54:54.758 SimplySolitaire[27643:473670] AGC retain == 3 12:54:54.798 SimplySolitaire[27643:473670] AGC retain == 4 12:54:54.798 SimplySolitaire[27643:473670] AGC retain == 5 12:54:54.798 SimplySolitaire[27643:473670] AGC retain == 6 12:54:54.798 SimplySolitaire[27643:473670] <<< DID actionSheet showInView: retain = 6 12:54:54.845 SimplySolitaire[27643:473670] AGC release = 5 12:54:54.891 SimplySolitaire[27643:473670] AGC release = 4 12:54:54.891 SimplySolitaire[27643:473670] AGC release = 3 // NOW ... dismiss the action sheet without selection (delegate is nil) 12:55:05.643 SimplySolitaire[27643:473670] AGC retain == 4 12:55:05.644 SimplySolitaire[27643:473670] AGC retain == 5 12:55:05.644 SimplySolitaire[27643:473670] AGC retain == 6 12:55:05.644 SimplySolitaire[27643:473670] AGC retain == 7 12:55:05.645 SimplySolitaire[27643:473670] AGC release = 6 12:55:05.645 SimplySolitaire[27643:473670] AGC release = 5 12:55:05.996 SimplySolitaire[27643:473670] AGC release = 4 12:55:05.997 SimplySolitaire[27643:473670] AGC release = 3 12:55:05.997 SimplySolitaire[27643:473670] AGC release = 2 // this is a correct retain of 2

答案 3 :(得分:-1)

我建议切换到UIAlertController。在iOS 8中不推荐使用UIActionSheet,所以你可以试一试,看看是否还有漏洞