Leaks with UIAlertController
Asked Answered
H

2

19

I added UIAlertController in my app by creating a category on UIViewController with the following method:

- (void)showAlertViewWithTitle:(NSString *)title
                       message:(NSString *)message
                       actions:(NSArray *)alertActions
{
   UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title ? : @"" message:message preferredStyle:UIAlertControllerStyleAlert];

   if (alertActions.count) {
      for (UIAlertAction *action in alertActions) {
         [alertController addAction:action];
      }
   } else {
      UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
      [alertController addAction:action];
   }

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

At first, everything looks great but when I analyze leaks with Instruments, each time I call this method, some leaks appear:

enter image description here

Here is how the call of showAlertViewWithTitle:message:actions: is done

[self showAlertViewWithTitle:nil message:@"Test message" actions:nil];

Any idea why I get all these leaks?

-- EDIT --

I tried the following in a sample project:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message"
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];

and I get the same leaks. I'm really not sure what's going on...

Heteroplasty answered 9/10, 2014 at 7:58 Comment(17)
I don't see any leak using xcode 6.Flavorous
What about Xcode 6.0.1?Heteroplasty
I am using 6.0.1, how did you find the leak? I don't see leak panel in instrumentsFlavorous
I saw a leak, sometimes it occurs.Flavorous
@CarouselMin - I am referring the this leak option in Instruments: cl.ly/image/040c0N0V4029Heteroplasty
Yes,I know,I tried that,it shows leaks once in a while, not reproducible every time I click the button to show the alert view.It shows a simple cycle of NSConstraints.Flavorous
I'm curious if in your test, your program was consisted of 3 viewControllers. I think it's a bad idea to have the functionality as a category, especially since it can be one line to [[[alloc] init] show], at 16 bytes per allocation, stored as an array, and automatically de-referenced when the reference count will reach to 0. Have you considered your results from Leaks to be a false positive?Ailssa
I also tried with a category, and I also get leaksHeteroplasty
Hmm... strange. I have the same issue. I just created a new simple Single View Application just to test this. The leak happens sporadically. Any luck with as to know why yet?Bisutun
It could be that the iOS library is responsible for the leak. I got the same kind of leak when I used an actionSheet. I chalked it up to how iOS 8 replaced uiAlertView and uiActionSheet with uiAlertController. I think they must have messed something up in the internals of uiAlertController and caused a leak.Salomo
Has anyone filed a bug report with apple?Hiss
@Hiss : I did on 09-Oct-2014, still waiting for an answerHeteroplasty
any news on this? I am still getting this leak with xcode Version 6.1.1 (6A2008a)Birmingham
@Birmingham - No, I'm still waiting for an answer from AppleHeteroplasty
Met the same problem.Distillate
https://mcmap.net/q/668627/-ios4-uialertview-why-this-code-causes-memory-leaksGushy
Are you running in Debug or Release? I spent ages looking for the mother and father of all leaks last year - only to discover that the leak dried up for a release build.Mcleroy
S
3

This is an iOS bug.

This is also a duplicate of SO question iOS 8 Only Memory Leak with UIAlertController or UIActionSheet posted 1 day earlier.

See Apple Bug Reporter issue 21005708, Memory leak in UIAlertController under ARC.

Streamlined answered 18/5, 2015 at 21:18 Comment(0)
H
-2

The leak seems to be fixed with iOS 8.2 and Xcode 6.2

Heteroplasty answered 13/3, 2015 at 8:33 Comment(1)
Won't leak on an actual device.Jennyjeno

© 2022 - 2024 — McMap. All rights reserved.