9

当我使用 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];

泄漏工具截图

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

4

4 回答 4

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];
于 2015-07-13T12:22:17.747 回答
4

这是一个 iOS 错误。

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

于 2015-05-18T21:15:35.323 回答
1

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

我自己通过覆盖视图控制器的保留和释放进行了一些测试,以显示 iOS 8.x 中的泄漏

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

  • 泄漏设备:iPad 3 (A1416)、iPad Air Simulator
  • 好的设备:iPhone 6 iOS 8.1.3,iPhone 4s 和 iOS 8.1.2

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

iPad Retina Simulator iOS 8.1 和真正的 iPad 泄漏 // 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 Simulator 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

于 2015-02-05T12:52:36.037 回答
-1

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

于 2014-10-08T00:24:56.503 回答