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...

MartinMoizard
  • 6,600
  • 12
  • 45
  • 75
  • I don't see any leak using xcode 6. – gabbler Oct 13 '14 at 14:49
  • What about Xcode 6.0.1? – MartinMoizard Oct 13 '14 at 15:17
  • I am using 6.0.1, how did you find the leak? I don't see leak panel in instruments – gabbler Oct 13 '14 at 15:30
  • I saw a leak, sometimes it occurs. – gabbler Oct 13 '14 at 15:36
  • @CarouselMin - I am referring the this leak option in Instruments: http://cl.ly/image/040c0N0V4029 – MartinMoizard Oct 14 '14 at 07:32
  • 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. – gabbler Oct 14 '14 at 08:01
  • 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? – aug2uag Nov 13 '14 at 00:15
  • I also tried with a category, and I also get leaks – MartinMoizard Nov 13 '14 at 10:53
  • 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? – shadowf Nov 20 '14 at 17:33
  • 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. – JS1 Nov 20 '14 at 18:50
  • Has anyone filed a bug report with apple? – dupuis2387 Nov 26 '14 at 14:44
  • @dupuis2387 : I did on 09-Oct-2014, still waiting for an answer – MartinMoizard Nov 26 '14 at 17:26
  • any news on this? I am still getting this leak with xcode Version 6.1.1 (6A2008a) – pasevin Dec 10 '14 at 16:27
  • @pasevin - No, I'm still waiting for an answer from Apple – MartinMoizard Dec 10 '14 at 16:33
  • Met the same problem. – Zigii Wong Dec 12 '14 at 06:40
  • http://stackoverflow.com/a/4304131/44964 – sabiland Dec 18 '14 at 11:55
  • 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. – headbanger Mar 05 '15 at 16:31

2 Answers2

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.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
-2

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

MartinMoizard
  • 6,600
  • 12
  • 45
  • 75