【问题标题】:iOS 8 Only Memory Leak with UIAlertController or UIActionSheetiOS 8 只有带有 UIAlertController 或 UIActionSheet 的内存泄漏
【发布时间】:2014-12-02 13:00:35
【问题描述】:

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

【问题讨论】:

标签: ios memory-leaks ios8 uiactionsheet uialertcontroller


【解决方案1】:

我建议在 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];

【讨论】:

    【解决方案2】:

    这是一个 iOS 错误。

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

    【讨论】:

      【解决方案3】:

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

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

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

      • LEAKY 设备:iPad 3 (A1416)、iPad Air Simulator
      • 好设备:iPhone 6 iOS 8.1.3、iPhone 4s 使用 iOS 8.1.2

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

      iPad 视网膜模拟器 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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 谢谢!我没有意识到这一点,xCode 也没有警告我。但是,UIAlertController 似乎显示了同样的问题:stackoverflow.com/questions/26273175/…
        • 确认使用 UIAlertController 显示相同的内存泄漏。
        • 你解决过这个问题吗?只是想知道这是否是另一个因素,(这就是为什么在您切换到 UIAlertController 后它仍然发生的原因)
        猜你喜欢
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多